Spring 整合rabbitmq 出现错误
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.amqp.rabbit.config.ListenerContainerFactoryBean#0': Invocation of init method failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1745)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:827)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:546)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:41010)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:400)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:291)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at org.springframework.amqp.rabbit.config.ListenerContainerFactoryBean.createContainer(ListenerContainerFactoryBean.java:533)
at org.springframework.amqp.rabbit.config.ListenerContainerFactoryBean.createInstance(ListenerContainerFactoryBean.java:411)
at org.springframework.amqp.rabbit.config.ListenerContainerFactoryBean.createInstance(ListenerContainerFactoryBean.java:61)
at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:142)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741)
... 22 more
rabbitmq.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
<!-- spring amqp默认的是jackson 的一个插件,目的将生产者生产的数据转换为json存入消息队列,由于Gson的速度快于jackson,这里替换为Gson的一个实现 -->
<bean id="jsonMessageConverter"
class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter"></bean>
<!-- 连接服务配置 -->
<rabbit:connection-factory
id="connectionFactory" host="127.0.0.1" username="wdg"
password="11111" port="5672" virtual-host="/" />
<rabbit:admin connection-factory="connectionFactory" />
<!-- spring template声明 -->
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" message-converter="jsonMessageConverter" />
<bean id="rabbbitmqutil" class="com.wdg.amqp.impl.RabbitmqService" />
<!-- queue 队列声明 -->
<rabbit:queue durable="true" auto-delete="false" exclusive="false" name="wdgqueue" />
<!-- exchange queue binging key 绑定 -->
<rabbit:direct-exchange name="wdgexchange" id="wdgexchange" durable="true" auto-delete="false">
<rabbit:bindings>
<rabbit:binding queue="wdgqueue" key="wdg" />
</rabbit:bindings>
</rabbit:direct-exchange>
<rabbit:queue durable="true" auto-delete="false" exclusive="false" name="fanoutqueue" />
<rabbit:queue durable="true" auto-delete="false" exclusive="false" name="fanoutqueue1" />
<rabbit:fanout-exchange id="fanoutexchange" name="fanoutexchange" durable="true" auto-delete="false">
<rabbit:bindings>
<rabbit:binding queue="fanoutqueue" />
<rabbit:binding queue="fanoutqueue1" />
</rabbit:bindings>
</rabbit:fanout-exchange>
<!-- 声明队列 -->
<rabbit:queue durable="true" auto-delete="false" exclusive="false" name="newsqueue" />
<rabbit:queue id="topicqueue" name="topicqueue" durable="true" auto-delete="false" exclusive="false" />
<rabbit:queue id="emailqueue" name="emailqueue" durable="true" auto-delete="false" exclusive="false" />
<!-- 声明一个topic主题 -->
<rabbit:topic-exchange name="exchange_topic"
durable="true" auto-delete="false" id="exchange_topic">
<rabbit:bindings>
<rabbit:binding queue="topicqueue" pattern="wdg.*" />
<rabbit:binding queue="emailqueue" pattern="email.*" />
<rabbit:binding queue="newsqueue" pattern="news.*" />
</rabbit:bindings>
</rabbit:topic-exchange>
<!-- topicqueue对应的消费者的配置 -->
<!-- <bean id="topiclistener" class="com.wdg.amqp.impl.TopicListener"></bean>
<bean id="receiveListenerAdapter" class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
<constructor-arg ref="topiclistener" />
<property name="defaultListenerMethod" value="onMessage"></property>
<property name="messageConverter" ref="jsonMessageConverter"></property>
</bean>
<bean id="listenerContainer" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
<property name="queueNames" value="topicqueue,emailqueue,wdgqueue,fanoutqueue"></property>
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="messageListener" ref="receiveListenerAdapter"></property>
</bean> -->
<bean id="consumeraListener" class="com.wdg.amqp.impl.ConsumeraListener"/>
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto" concurrency="1" >
<rabbit:listener queues="newsqueue" method="onMessage" ref="consumeraListener"/>
</rabbit:listener-container>
</beans>
修改:
版本号去掉
希望对你有所帮助