Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
629ae0f5
Commit
629ae0f5
authored
Aug 06, 2021
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改实例化关系
parent
53e28b4e
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
191 additions
and
74 deletions
+191
-74
BeanDao.java
...util-mq/src/main/java/com/yanzuoguang/mq/dao/BeanDao.java
+26
-8
MqMessageInitPlan.java
.../main/java/com/yanzuoguang/mq/plan/MqMessageInitPlan.java
+24
-3
YzgMqConsumer.java
.../src/main/java/com/yanzuoguang/mq/plan/YzgMqConsumer.java
+27
-7
YzgMqProcedure.java
...src/main/java/com/yanzuoguang/mq/plan/YzgMqProcedure.java
+23
-3
MessageServiceImpl.java
...a/com/yanzuoguang/mq/service/impl/MessageServiceImpl.java
+26
-7
MqServiceImpl.java
...n/java/com/yanzuoguang/mq/service/impl/MqServiceImpl.java
+38
-36
QueueServiceImpl.java
...ava/com/yanzuoguang/mq/service/impl/QueueServiceImpl.java
+27
-10
No files found.
yzg-util-mq/src/main/java/com/yanzuoguang/mq/dao/BeanDao.java
View file @
629ae0f5
...
...
@@ -5,10 +5,11 @@ import com.yanzuoguang.util.exception.CodeException;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.log.Log
;
import
org.springframework.amqp.core.*
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.BeanInitializationException
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.beans.factory.support.DefaultListableBeanFactory
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.*
;
import
org.springframework.stereotype.Component
;
import
java.util.HashMap
;
...
...
@@ -20,7 +21,7 @@ import java.util.Map;
* @author 颜佐光
*/
@Component
public
class
BeanDao
{
public
class
BeanDao
implements
ApplicationContextAware
{
private
static
final
String
QUEUE
=
"queue"
;
private
static
final
String
EXCHANGE
=
"exchange"
;
...
...
@@ -28,12 +29,29 @@ public class BeanDao {
/**
* 上下文
*/
@Autowired
private
ApplicationContext
context
;
@Autowired
private
AmqpAdmin
amqpAdmin
;
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Override
public
void
setApplicationContext
(
ApplicationContext
applicationContext
)
throws
BeansException
{
context
=
applicationContext
.
getBean
(
ApplicationContext
.
class
);
amqpAdmin
=
applicationContext
.
getBean
(
AmqpAdmin
.
class
);
}
/**
* 获取队列是否存在的实体
*
...
...
yzg-util-mq/src/main/java/com/yanzuoguang/mq/plan/MqMessageInitPlan.java
View file @
629ae0f5
...
...
@@ -6,8 +6,11 @@ import com.yanzuoguang.util.helper.StringHelper;
import
com.yanzuoguang.util.log.Log
;
import
com.yanzuoguang.util.thread.ThreadHelper
;
import
com.yanzuoguang.util.thread.ThreadNext
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.BeanInitializationException
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.*
;
import
org.springframework.stereotype.Component
;
import
java.util.List
;
...
...
@@ -18,9 +21,8 @@ import java.util.List;
* @author 颜佐光
*/
@Component
public
class
MqMessageInitPlan
implements
ThreadNext
.
Next
,
Runnable
{
public
class
MqMessageInitPlan
implements
ThreadNext
.
Next
,
Runnable
,
ApplicationContextAware
{
@Autowired
private
MessageService
messageService
;
@Value
(
"${yzg.mq.retry.size:100}"
)
...
...
@@ -29,6 +31,25 @@ public class MqMessageInitPlan implements ThreadNext.Next, Runnable {
@Value
(
"${yzg.mq.retry.time:60000}"
)
private
int
retryTime
=
1000
;
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Override
public
void
setApplicationContext
(
ApplicationContext
applicationContext
)
throws
BeansException
{
messageService
=
applicationContext
.
getBean
(
MessageService
.
class
);
}
/**
* 是否为空
*/
...
...
yzg-util-mq/src/main/java/com/yanzuoguang/mq/plan/YzgMqConsumer.java
View file @
629ae0f5
...
...
@@ -11,7 +11,10 @@ import com.yanzuoguang.util.helper.JsonHelper;
import
com.yanzuoguang.util.log.Log
;
import
org.springframework.amqp.core.Message
;
import
org.springframework.amqp.rabbit.annotation.RabbitListener
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.BeanInitializationException
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.context.*
;
import
org.springframework.stereotype.Component
;
/**
...
...
@@ -20,17 +23,34 @@ import org.springframework.stereotype.Component;
* @author 颜佐光
*/
@Component
public
class
YzgMqConsumer
{
public
class
YzgMqConsumer
implements
ApplicationContextAware
{
@Autowired
private
MqService
mqService
;
@Autowired
private
YzgMqProcedure
yzgMqProcedure
;
@Autowired
private
QueueService
queueService
;
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Override
public
void
setApplicationContext
(
ApplicationContext
applicationContext
)
throws
BeansException
{
mqService
=
applicationContext
.
getBean
(
MqService
.
class
);
yzgMqProcedure
=
applicationContext
.
getBean
(
YzgMqProcedure
.
class
);
queueService
=
applicationContext
.
getBean
(
QueueService
.
class
);
}
/**
* MQ回调
*
...
...
yzg-util-mq/src/main/java/com/yanzuoguang/mq/plan/YzgMqProcedure.java
View file @
629ae0f5
...
...
@@ -9,9 +9,11 @@ import com.yanzuoguang.util.exception.CodeException;
import
com.yanzuoguang.util.helper.DateHelper
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.BeanInitializationException
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.*
;
import
org.springframework.stereotype.Component
;
import
java.util.ArrayList
;
...
...
@@ -24,7 +26,7 @@ import java.util.List;
* @author 颜佐光
*/
@Component
public
class
YzgMqProcedure
implements
InitializingBean
{
public
class
YzgMqProcedure
implements
InitializingBean
,
ApplicationContextAware
{
/**
* 执行的消息队列
*/
...
...
@@ -53,12 +55,30 @@ public class YzgMqProcedure implements InitializingBean {
/**
* MQ服务
*/
@Autowired
private
MqService
mqService
;
@Value
(
"${yzg.mq.unit.min:1000}"
)
private
long
min
;
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Override
public
void
setApplicationContext
(
ApplicationContext
applicationContext
)
throws
BeansException
{
mqService
=
applicationContext
.
getBean
(
MqService
.
class
);
}
/**
* Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and ApplicationContextAware).
...
...
yzg-util-mq/src/main/java/com/yanzuoguang/mq/service/impl/MessageServiceImpl.java
View file @
629ae0f5
...
...
@@ -13,7 +13,10 @@ import org.springframework.amqp.core.MessageDeliveryMode;
import
org.springframework.amqp.core.MessagePostProcessor
;
import
org.springframework.amqp.core.MessageProperties
;
import
org.springframework.amqp.rabbit.support.CorrelationData
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.BeanInitializationException
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.context.*
;
import
org.springframework.stereotype.Component
;
import
org.springframework.transaction.annotation.Transactional
;
...
...
@@ -27,20 +30,36 @@ import java.util.List;
* @author 颜佐光
*/
@Component
public
class
MessageServiceImpl
implements
MessageService
{
public
class
MessageServiceImpl
implements
MessageService
,
ApplicationContextAware
{
/**
* 用于内部自引用,调用事物
*/
@Autowired
private
MessageDao
messageDao
;
@Autowired
private
MyRabbitTemplate
rabbitTemplate
;
@Autowired
private
YzgMqProcedure
yzgMqProcedure
;
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Override
public
void
setApplicationContext
(
ApplicationContext
applicationContext
)
throws
BeansException
{
messageDao
=
applicationContext
.
getBean
(
MessageDao
.
class
);
rabbitTemplate
=
applicationContext
.
getBean
(
MyRabbitTemplate
.
class
);
yzgMqProcedure
=
applicationContext
.
getBean
(
YzgMqProcedure
.
class
);
}
/**
* 打上批次
*
...
...
yzg-util-mq/src/main/java/com/yanzuoguang/mq/service/impl/MqServiceImpl.java
View file @
629ae0f5
...
...
@@ -22,8 +22,10 @@ import io.swagger.annotations.ApiOperation;
import
org.springframework.amqp.core.Message
;
import
org.springframework.amqp.rabbit.core.ChannelAwareMessageListener
;
import
org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.BeanInitializationException
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.
beans.factory.annotation.Autowired
;
import
org.springframework.
context.*
;
import
org.springframework.stereotype.Component
;
import
java.io.IOException
;
...
...
@@ -36,44 +38,49 @@ import java.util.List;
* @author 颜佐光
*/
@Component
public
class
MqServiceImpl
implements
MqService
,
InitializingBean
{
public
class
MqServiceImpl
implements
MqService
,
InitializingBean
,
ApplicationContextAware
{
@Autowired
private
QueueService
queueService
;
@Autowired
private
MessageService
messageService
;
@Autowired
private
MqConsumeDynamic
mqConsumeDynamic
;
@Autowired
private
QueueServerDao
queueServerDao
;
@Autowired
private
QueueServerTokenDao
queueServerTokenDao
;
@Autowired
private
MessageLogDao
messageLogDao
;
@Autowired
private
YzgMqProcedure
yzgMqProcedure
;
private
String
localName
=
""
;
private
String
getLocalName
(
String
name
)
{
return
this
.
getServerName
(
name
,
this
.
localName
);
}
private
String
getServerName
(
String
name
,
String
serverId
)
{
return
String
.
format
(
"%s:%s"
,
name
,
serverId
);
}
/**
* 是否初始化
*/
private
boolean
init
=
false
;
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Override
public
void
setApplicationContext
(
ApplicationContext
applicationContext
)
throws
BeansException
{
queueService
=
applicationContext
.
getBean
(
QueueService
.
class
);
messageService
=
applicationContext
.
getBean
(
MessageService
.
class
);
mqConsumeDynamic
=
applicationContext
.
getBean
(
MqConsumeDynamic
.
class
);
queueServerDao
=
applicationContext
.
getBean
(
QueueServerDao
.
class
);
queueServerTokenDao
=
applicationContext
.
getBean
(
QueueServerTokenDao
.
class
);
messageLogDao
=
applicationContext
.
getBean
(
MessageLogDao
.
class
);
yzgMqProcedure
=
applicationContext
.
getBean
(
YzgMqProcedure
.
class
);
this
.
localName
=
UrlHelper
.
getIp
();
}
/**
* Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and ApplicationContextAware).
...
...
@@ -86,20 +93,17 @@ public class MqServiceImpl implements MqService, InitializingBean {
*/
@Override
public
void
afterPropertiesSet
()
throws
Exception
{
this
.
init
();
}
public
synchronized
void
init
()
{
if
(
this
.
init
)
{
return
;
}
this
.
localName
=
UrlHelper
.
getIp
();
QueueVo
removeToken
=
new
QueueVo
(
YzgMqProcedure
.
YZG_MQ_CLEAR_TOKEN_QUEUE
);
removeToken
.
check
();
queueService
.
create
(
removeToken
);
}
private
String
getLocalName
(
String
name
)
{
return
this
.
getServerName
(
name
,
this
.
localName
);
}
this
.
init
=
true
;
private
String
getServerName
(
String
name
,
String
serverId
)
{
return
String
.
format
(
"%s:%s"
,
name
,
serverId
);
}
/**
...
...
@@ -109,7 +113,6 @@ public class MqServiceImpl implements MqService, InitializingBean {
*/
@Override
public
String
createQueue
(
QueueVo
req
)
{
this
.
init
();
req
.
check
();
queueService
.
create
(
req
);
return
"创建成功"
;
...
...
@@ -181,7 +184,6 @@ public class MqServiceImpl implements MqService, InitializingBean {
*/
@Override
public
String
message
(
MessageVo
req
,
boolean
now
)
{
this
.
init
();
req
.
check
();
return
messageService
.
send
(
req
,
now
);
}
...
...
yzg-util-mq/src/main/java/com/yanzuoguang/mq/service/impl/QueueServiceImpl.java
View file @
629ae0f5
...
...
@@ -8,7 +8,10 @@ import com.yanzuoguang.mq.service.QueueService;
import
com.yanzuoguang.mq.vo.QueueVo
;
import
com.yanzuoguang.mq.vo.req.MessageLogReqVo
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.BeanInitializationException
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.context.*
;
import
org.springframework.stereotype.Component
;
import
org.springframework.transaction.annotation.Transactional
;
...
...
@@ -18,20 +21,35 @@ import org.springframework.transaction.annotation.Transactional;
* @author 颜佐光
*/
@Component
public
class
QueueServiceImpl
implements
QueueService
{
public
class
QueueServiceImpl
implements
QueueService
,
ApplicationContextAware
{
@Autowired
private
BeanDao
beanDao
;
@Autowired
private
MessageLogDao
messageLogDao
;
@Autowired
private
MqService
mqService
;
@Autowired
private
YzgMqProcedure
mqProcedure
;
/**
* Set the ApplicationContext that this object runs in.
* Normally this call will be used to initialize the object.
* <p>Invoked after population of normal bean properties but before an init callback such
* as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
* {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
* {@link MessageSourceAware}, if applicable.
*
* @param applicationContext the ApplicationContext object to be used by this object
* @throws ApplicationContextException in case of context initialization errors
* @throws BeansException if thrown by application context methods
* @see BeanInitializationException
*/
@Override
public
void
setApplicationContext
(
ApplicationContext
applicationContext
)
throws
BeansException
{
beanDao
=
applicationContext
.
getBean
(
BeanDao
.
class
);
messageLogDao
=
applicationContext
.
getBean
(
MessageLogDao
.
class
);
mqService
=
applicationContext
.
getBean
(
MqService
.
class
);
mqProcedure
=
applicationContext
.
getBean
(
YzgMqProcedure
.
class
);
}
/**
* 保存接口请求日志
*
...
...
@@ -86,5 +104,4 @@ public class QueueServiceImpl implements QueueService {
// 创建绑定队列
beanDao
.
createBinding
(
vo
.
getExchangeName
(),
vo
.
getQueueName
(),
vo
.
getRouteKey
());
}
}
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