Commit 629ae0f5 authored by yanzg's avatar yanzg

修改实例化关系

parent 53e28b4e
......@@ -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);
}
/**
* 获取队列是否存在的实体
*
......
......@@ -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);
}
/**
* 是否为空
*/
......
......@@ -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回调
*
......
......@@ -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).
......
......@@ -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);
}
/**
* 打上批次
*
......
......@@ -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);
}
......
......@@ -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());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment