Commit 1583439b authored by yanzg's avatar yanzg

升级新版本

parent 9d34f592
......@@ -4,6 +4,7 @@ import com.yanzuoguang.dao.DaoConst;
import com.yanzuoguang.util.YzgError;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.helper.YzgTimeout;
import com.yanzuoguang.util.vo.Ref;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
......@@ -82,13 +83,21 @@ public class BeanDao {
* @return 创建成功的队列
*/
public Queue createQueue(String queueName, long deadTime, String deadExchange, String deadRouteKey) {
Ref<Queue> ret = new Ref<>(null);
YzgTimeout.timeOut(BeanDao.class, "创建队列" + queueName, () ->
ret.value = createQueueRun(queueName, deadTime, deadExchange, deadRouteKey)
);
// 重新获取实体
return ret.value;
}
private Queue createQueueRun(String queueName, long deadTime, String deadExchange, String deadRouteKey) {
String key = StringHelper.getId(QUEUE, queueName);
// 判断队列是否存在,不存在则锁定再次判断
Queue queueHis = getBean(Queue.class, key);
if (queueHis != null) {
return queueHis;
}
// 创建队列实体
Map<String, Object> params = new HashMap<>(DaoConst.COLLECTION_INIT_SIZE);
if (!StringHelper.isEmpty(deadExchange) && !StringHelper.isEmpty(deadRouteKey)) {
......@@ -100,14 +109,12 @@ public class BeanDao {
// x-dead-letter-routing-key 声明了这些死信在转发时携带的 routing-key 名称。
params.put("x-dead-letter-routing-key", deadRouteKey);
}
// 创建实体
Queue queueNew = new Queue(queueName, true, false, false, params);
YzgTimeout.timeOut(BeanDao.class, "创建队列" + queueName, () -> {
amqpAdmin.declareQueue(queueNew);
});
amqpAdmin.declareQueue(queueNew);
// 将实体注册到上下文中
register(key, queueNew);
// 重新获取实体
return queueNew;
}
......@@ -118,16 +125,24 @@ public class BeanDao {
* @return 创建成功的交换器
*/
public TopicExchange createExchange(String exchangeName) {
Ref<TopicExchange> ret = new Ref<>(null);
YzgTimeout.timeOut(BeanDao.class, "创建交换器" + exchangeName, () ->
ret.value = createExchangeRun(exchangeName)
);
// 重新获取实体
return ret.value;
}
private TopicExchange createExchangeRun(String exchangeName) {
String key = StringHelper.getId(EXCHANGE, exchangeName);
// 判断队列是否存在,不存在则锁定再次判断
TopicExchange beanHis = getBean(TopicExchange.class, key);
if (beanHis != null) {
return beanHis;
}
// 创建实体
TopicExchange bean = new TopicExchange(exchangeName, true, false);
YzgTimeout.timeOut(BeanDao.class, "创建交换器" + exchangeName, () -> amqpAdmin.declareExchange(bean));
amqpAdmin.declareExchange(bean);
// 将实体注册到上下文中
register(key, bean);
......@@ -144,11 +159,20 @@ public class BeanDao {
* @return 绑定对象
*/
public Binding createBinding(String exchangeName, String queueName, String routeKey) {
Ref<Binding> ret = new Ref<>(null);
YzgTimeout.timeOut(BeanDao.class, "创建路由绑定" + routeKey, () ->
ret.value = createBindingRun(exchangeName, queueName, routeKey)
);
return ret.value;
}
public Binding createBindingRun(String exchangeName, String queueName, String routeKey) {
Binding binding = BindingBuilder.bind(getQueue(queueName)).to(getExchange(exchangeName)).with(routeKey);
YzgTimeout.timeOut(BeanDao.class, "创建路由绑定" + routeKey, () -> amqpAdmin.declareBinding(binding));
amqpAdmin.declareBinding(binding);
return binding;
}
/**
* 获取实体
*
......
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