动态添加队列
							parent
							
								
									d1e9a65aca
								
							
						
					
					
						commit
						38a122ca60
					
				| @ -0,0 +1,52 @@ | |||||||
|  | package com.glxp.api.service.trace; | ||||||
|  | 
 | ||||||
|  | import cn.hutool.core.util.StrUtil; | ||||||
|  | import lombok.extern.slf4j.Slf4j; | ||||||
|  | import org.springframework.amqp.core.AmqpAdmin; | ||||||
|  | import org.springframework.amqp.core.BindingBuilder; | ||||||
|  | import org.springframework.amqp.core.FanoutExchange; | ||||||
|  | import org.springframework.amqp.core.Queue; | ||||||
|  | import org.springframework.beans.factory.annotation.Autowired; | ||||||
|  | import org.springframework.stereotype.Service; | ||||||
|  | 
 | ||||||
|  | import javax.annotation.Resource; | ||||||
|  | import java.util.Properties; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * 动态创建队列 | ||||||
|  |  */ | ||||||
|  | @Slf4j | ||||||
|  | @Service | ||||||
|  | public class RabbitQueueService { | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     @Resource | ||||||
|  |     private AmqpAdmin amqpAdmin; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     public void createQueue(String queueName) { | ||||||
|  |         //判断队列是否存在
 | ||||||
|  |         if (!isExistQueue(queueName)) { | ||||||
|  |             Queue queue = new Queue(queueName, true); | ||||||
|  |             FanoutExchange fanoutExchange = new FanoutExchange(RabbitPushService.FANOUT_EXCHANGE); | ||||||
|  |             amqpAdmin.declareQueue(queue); | ||||||
|  |             amqpAdmin.declareExchange(fanoutExchange); | ||||||
|  |             amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(fanoutExchange)); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public boolean isExistQueue(String queueName) { | ||||||
|  |         boolean flag = true; | ||||||
|  |         if (StrUtil.isNotBlank(queueName)) { | ||||||
|  |             Properties queueProperties = amqpAdmin.getQueueProperties(queueName); | ||||||
|  |             if (queueProperties == null) { | ||||||
|  |                 flag = false; | ||||||
|  |             } | ||||||
|  |         } else { | ||||||
|  |             throw new RuntimeException("队列名称为空"); | ||||||
|  |         } | ||||||
|  |         return flag; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
					Loading…
					
					
				
		Reference in New Issue