Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
commons
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
3
Merge Requests
3
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DevOps
commons
Commits
048836e6
Commit
048836e6
authored
Feb 06, 2018
by
ag
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1. 修复IDGenerator 的DataCenterId 默认不存在无法启动的问题
2. 默认增加MQ自动停止.
parent
1f42d4a5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
19 additions
and
8 deletions
+19
-8
BaseDestroyHandler.java
.../java/cn/quantgroup/tech/shutdown/BaseDestroyHandler.java
+16
-5
GracefulShutdownProperties.java
.../tech/shutdown/properties/GracefulShutdownProperties.java
+0
-1
IDGenerator.java
...ng/src/main/java/cn/quantgroup/tech/util/IDGenerator.java
+3
-2
No files found.
commons-spring/src/main/java/cn/quantgroup/tech/shutdown/BaseDestroyHandler.java
View file @
048836e6
...
@@ -2,12 +2,22 @@ package cn.quantgroup.tech.shutdown;
...
@@ -2,12 +2,22 @@ package cn.quantgroup.tech.shutdown;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.amqp.rabbit.config.RabbitListenerConfigUtils
;
import
org.springframework.amqp.rabbit.config.RabbitListenerConfigUtils
;
import
org.springframework.amqp.rabbit.core.RabbitTemplate
;
import
org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry
;
import
org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnBean
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.Lifecycle
;
import
org.springframework.context.Lifecycle
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.PreDestroy
;
/**
* 这里是一些基本的停止工具. to be continue
*
* @author ag
*/
@Slf4j
@Slf4j
@Component
@Component
public
class
BaseDestroyHandler
{
public
class
BaseDestroyHandler
{
...
@@ -15,14 +25,14 @@ public class BaseDestroyHandler {
...
@@ -15,14 +25,14 @@ public class BaseDestroyHandler {
@Autowired
@Autowired
private
ApplicationContext
applicationContext
;
private
ApplicationContext
applicationContext
;
//todo test
@PreDestroy
// @PreDestroy
@ConditionalOnBean
(
RedisTemplate
.
class
)
private
void
stopRedisSub
()
{
private
void
stopRedisSub
()
{
log
.
info
(
"redis stopped"
);
log
.
info
(
"
我什么都没做,
redis stopped"
);
}
}
//todo test
@PreDestroy
// @PreDestroy
@ConditionalOnBean
(
RabbitTemplate
.
class
)
private
void
stopRabbitMQ
()
{
private
void
stopRabbitMQ
()
{
try
{
try
{
RabbitListenerEndpointRegistry
rabbitListenerEndpointRegistry
=
applicationContext
.
getBean
(
RabbitListenerEndpointRegistry
rabbitListenerEndpointRegistry
=
applicationContext
.
getBean
(
...
@@ -30,6 +40,7 @@ public class BaseDestroyHandler {
...
@@ -30,6 +40,7 @@ public class BaseDestroyHandler {
RabbitListenerEndpointRegistry
.
class
);
RabbitListenerEndpointRegistry
.
class
);
rabbitListenerEndpointRegistry
.
getListenerContainers
().
forEach
(
Lifecycle:
:
stop
);
rabbitListenerEndpointRegistry
.
getListenerContainers
().
forEach
(
Lifecycle:
:
stop
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
log
.
error
(
"貌似停止 MQ 遇到了问题..."
);
}
}
log
.
info
(
"MQ listener stopped"
);
log
.
info
(
"MQ listener stopped"
);
}
}
...
...
commons-spring/src/main/java/cn/quantgroup/tech/shutdown/properties/GracefulShutdownProperties.java
View file @
048836e6
...
@@ -13,7 +13,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
...
@@ -13,7 +13,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@Setter
@Setter
@ConfigurationProperties
(
prefix
=
"shutdown.graceful"
)
@ConfigurationProperties
(
prefix
=
"shutdown.graceful"
)
public
class
GracefulShutdownProperties
{
public
class
GracefulShutdownProperties
{
/**
/**
* The timeout before force shutdown. TimeUnit.Second
* The timeout before force shutdown. TimeUnit.Second
*/
*/
...
...
commons-spring/src/main/java/cn/quantgroup/tech/util/IDGenerator.java
View file @
048836e6
...
@@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j;
...
@@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
...
@@ -33,6 +34,7 @@ import java.util.concurrent.locks.ReentrantLock;
...
@@ -33,6 +34,7 @@ import java.util.concurrent.locks.ReentrantLock;
@Slf4j
@Slf4j
@Component
@Component
@ConditionalOnBean
(
RedisTemplate
.
class
)
@ConditionalOnBean
(
RedisTemplate
.
class
)
@ConditionalOnProperty
(
name
=
"data.center.id"
)
public
class
IDGenerator
{
public
class
IDGenerator
{
private
static
final
String
REDIS_WORK_ID_KEY
=
"GLOBAL:WORK:ID:"
;
private
static
final
String
REDIS_WORK_ID_KEY
=
"GLOBAL:WORK:ID:"
;
...
@@ -88,13 +90,12 @@ public class IDGenerator {
...
@@ -88,13 +90,12 @@ public class IDGenerator {
private
StringRedisTemplate
redis
;
private
StringRedisTemplate
redis
;
@Value
(
"${data.center.id}"
)
@Value
(
"${data.center.id}"
)
public
void
setDataCenterId
(
int
dataCenterId
)
{
public
void
setDataCenterId
(
Integer
dataCenterId
)
{
DATA_CENTER_ID
=
dataCenterId
;
DATA_CENTER_ID
=
dataCenterId
;
}
}
@PostConstruct
@PostConstruct
public
void
init
()
{
public
void
init
()
{
log
.
info
(
"test Init id generator"
);
int
workerId
=
(
int
)
(
redis
.
opsForValue
().
increment
(
REDIS_WORK_ID_KEY
+
DATA_CENTER_ID
,
1
)
%
MAX_WORK_ID
);
int
workerId
=
(
int
)
(
redis
.
opsForValue
().
increment
(
REDIS_WORK_ID_KEY
+
DATA_CENTER_ID
,
1
)
%
MAX_WORK_ID
);
WORKER_ID_STR
=
String
.
format
(
"%04d"
,
workerId
);
WORKER_ID_STR
=
String
.
format
(
"%04d"
,
workerId
);
DATA_CENTER_STR
=
String
.
format
(
"%03d"
,
DATA_CENTER_ID
);
DATA_CENTER_STR
=
String
.
format
(
"%03d"
,
DATA_CENTER_ID
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment