- 配置rabbitmq
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd" >
<description>rabbitmq 連接服務配置</description>
<bean id="rabbitMqConnectionFactory"
class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<property name="addresses" value="${rabbitmq.addresses}"/>
<property name="username" value="${rabbitmq.username}"/>
<property name="password" value="${rabbitmq.password}"/>
<property name="virtualHost" value="${rabbitmq.vhost}"/>
<property name="channelCacheSize" value="${rabbitmq.channelCacheSize}"/>
</bean>
<rabbit:template id="amqpTemplate" connection-factory="rabbitMqConnectionFactory"
/>
</beans>
- 工具類實現
@Slf4j
public class RabbitMqUtil{
private static RabbitTemplate amqpTemplate;
static {
amqpTemplate = SpringContextUtil.getBean(RabbitTemplate.class);
Assert.notNull(amqpTemplate,"獲取不到amqpTemplate");
}
public static BaseResultDTO getMessageCount(String exchange, String exchangeType, String quene) {
Assert.hasText(exchange,"exchange不能爲空");
Assert.hasText(quene,"隊列名不能爲空");
Assert.hasText(exchangeType,"exchangeType不能爲空");
checkInitSuccess();
BaseResultDTO baseResultDTO = new BaseResultDTO();
ConnectionFactory connectionFactory = amqpTemplate.getConnectionFactory();
Connection connection = connectionFactory.createConnection();
Channel channel = connection.createChannel(false);
try {
channel.exchangeDeclare(exchange, exchangeType, true, false, null);
AMQP.Queue.DeclareOk declareOk = channel.queueDeclarePassive(quene);
Integer queueCount = declareOk.getMessageCount();
channel.close();
connection.close();
baseResultDTO.setSuccess(true);
baseResultDTO.setMessage(queueCount.toString());
} catch (IOException e) {
LoggerFormatUtil.warn(e,log,"連接rabbitmq異常");
}
return baseResultDTO;
}
private static void checkInitSuccess(){
if (null == amqpTemplate){
amqpTemplate = SpringContextUtil.getBean(RabbitTemplate.class);
}
Assert.notNull(amqpTemplate, "amqpTemplate 實例化異常");
}
}
- spring輔助工具類獲取bean
@Component
public class SpringContextUtil implements ApplicationContextAware {
public static ApplicationContext applicationContext = null;
public SpringContextUtil() {
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String beanName, Class<T> beanType) {
Assert.isTrue(applicationContext != null, "應用上下文不能爲空");
Object bean = applicationContext.getBean(beanName);
return bean == null ? null : (T)bean;
}
@SuppressWarnings("unchecked")
public static <T> T getBean(Class<T> beanType){
Assert.isTrue(applicationContext != null, "應用上下文不能爲空");
T bean = applicationContext.getBean(beanType);
return bean == null ? null : bean;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
}