<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    初一七月

    你必須承認,我們生存的這個世界取決于自身的能力而非別人的保護(AW)
    隨筆 - 23, 文章 - 0, 評論 - 11, 引用 - 0
    數據加載中……

    ActiveMQ報Address already in use異常

    使用Spring集成ActiveMQ時,可以使用如下配置
    <bean id="connectionFactory"
            class
    ="org.apache.activemq.ActiveMQConnectionFactory">
            
    <property name="brokerURL" value="${brokerURL}" />
    </bean>
    <bean id="queueDestination"
            class
    ="org.apache.activemq.command.ActiveMQQueue">
            
    <constructor-arg index="0" value="caojh" />
    </bean>
    <bean id="jmsTemplate"
            class
    ="org.springframework.jms.core.JmsTemplate">
            
    <property name="connectionFactory" ref="pooledConnectionFactory" />
            
    <property name="defaultDestination" ref="queueDestination"></property>
            
    <property name="messageConverter" ref="voteMsgConverter"></property>
    </bean>
    <bean id="voteMsgConverter"
            class
    ="com.netease.queue.domain.VoteMsgConverter">
    </bean>

    然后使用如下的代碼發送message
        public void templateSend(long id, String location) {
            JmsTemplate template 
    = (JmsTemplate) context.getBean("jmsTemplate");

             Vote vote 
    = new Vote();
             vote.setId(id);
             vote.setUserid(
    "caojh");
             vote.setLocation(location);
             template.convertAndSend(vote);
        }

    發送消息是沒有問題的,但是當密集發送大量消息時,會拋出地址占用,無法創建connection(或者在某些較老版本下,當消息數達到65535條的時候,也會無法再次發送,只能重啟隊列),具體異常如下
    org.springframework.jms.UncategorizedJmsException: Uncategorized exception occured during JMS
     processing; nested exception is javax.jms.JMSException: Could not connect to broker 
    URL: tcp:
    //192.168.20.23:61616. Reason: java.net.BindException: Address already in use: connect
        at org.springframework.jms.support.JmsUtils.convertJmsAccessException(JmsUtils.java:316)
        at org.springframework.jms.support.JmsAccessor.convertJmsAccessException(JmsAccessor.java:
    168)
        at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:
    469)
        at org.springframework.jms.core.JmsTemplate.send(JmsTemplate.java:
    534)
        at net.kentop.astoam.device.MG800DeviceService.excute(MG800DeviceService.java:
    423)
        at net.kentop.astoam.device.MG800DeviceService$HandlerReceiveMessage.
    handlerUdpData(MG800DeviceService.java:
    936)
        at net.kentop.mon4mg.monitor.UDPReceiverThread.run(UDPReceiverThread.java:
    51)
    Caused by: javax.jms.JMSException: Could not connect to broker URL: tcp:
    //localhost:61616. 
    Reason: java.net.BindException: Address already in use: connect
        at org.apache.activemq.util.JMSExceptionSupport.create(JMSExceptionSupport.java:
    35)
        at org.apache.activemq.ActiveMQConnectionFactory.
    createActiveMQConnection(ActiveMQConnectionFactory.java:
    286)
        at org.apache.activemq.ActiveMQConnectionFactory.
    createActiveMQConnection(ActiveMQConnectionFactory.java:
    230)
        at org.apache.activemq.ActiveMQConnectionFactory.
    createConnection(ActiveMQConnectionFactory.java:
    178)
        at org.springframework.jms.support.JmsAccessor.createConnection(JmsAccessor.java:
    184)
        at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:
    456)
         
    4 more

    無法發送的真正原因就在這里了
    http://activemq.apache.org/jmstemplate-gotchas.html

    這里有這么兩段話
    The thing to remember is JmsTemplate is designed for use in EJBs using the EJB containers JMS pooling abstraction. So every method will typically create a connection, session, producer or consumer, do something, then close them all down again. The idea being that this will use the J2EE containers pooling mechanism to pool the JMS resources under the covers. Without using a pooled JMS provider from the EJB container this is the worst possible way of working with JMS; since typically each create/close of a connection, producer/consumer results in a request-response with the JMS broker.

    You should only use JmsTemplate with a pooled JMS provider. In J2EE 1.4 or later that typically means a JCA based JMS ConnectionFactory. If you are in an EJB then make sure you use your J2EE containers ConnectionFactory, never a plain-old-connection factory. If you are not inside an EJB Then you should use our PooledConnectionFactory, then things will be nicely pooled. If you need to take part in XA transactions then look into our spring based JCA Container.

    大致意思就是說,每次發送一條消息時,都會創建一個connection和session,發送/接收完畢后再全部銷毀。如果沒有相應的pool機制,要發送大量消息,就會頻繁的創建、銷毀連接,這將是一個相當糟糕的選擇。

    至于解決的方法,第二段話也說的很清楚,要不就使用EJB容器的ConnectionFactory,要么就使用ActiveMQ提供的PooledConnectionFactory,這個類其實也是實現了ConnectionFactory接口

    使用如下的配置,就可以避免上面的異常了
    <bean id="pooledConnectionFactory"
            class
    ="org.apache.activemq.pool.PooledConnectionFactory">
            
    <property name="connectionFactory" ref="connectionFactory" />
    </bean>

    <bean id="connectionFactory"
            class
    ="org.apache.activemq.ActiveMQConnectionFactory">
            
    <property name="brokerURL" value="${brokerURL}" />
    </bean>

    【題外話】是否可以使用同一個connection并發發送/接收消息

    可以參考 http://activemq.apache.org/can-i-send-and-receive-messages-concurrently-on-one-jms-connection.html
    文中寫的很明白,每個發送者/接收者應該使用獨立的session,每個connection可以隨意創建任意多個session


    posted on 2012-07-27 15:59 初一七月 閱讀(3881) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 日韩在线一区二区三区免费视频 | 国产激情免费视频在线观看| 国产做床爱无遮挡免费视频| 亚洲国产成人精品无码区二本| 成人免费毛片观看| 在线亚洲高清揄拍自拍一品区| 毛片a级毛片免费播放100| 亚洲国产成人久久综合| 最新69国产成人精品免费视频动漫| 亚洲另类无码一区二区三区| 免费萌白酱国产一区二区| 视频一区在线免费观看| 永久亚洲成a人片777777| 成全视频免费观看在线看| 亚洲AV第一页国产精品| 我的小后妈韩剧在线看免费高清版| 中文字幕无码亚洲欧洲日韩| 免费人成在线观看播放国产| 国产精品福利在线观看免费不卡| 亚洲尹人九九大色香蕉网站| 黄页网站免费观看| 在线观看亚洲电影| 国产l精品国产亚洲区在线观看| 777成影片免费观看| 亚洲精品理论电影在线观看| 亚洲人成网站18禁止一区| 国产一级淫片a免费播放口| 亚洲成年人电影在线观看| 国产三级免费电影| 日韩免费视频一区二区| 亚洲中文字幕人成乱码| 亚洲成av人片不卡无码久久| 97精品免费视频| 国产亚洲综合精品一区二区三区| 国产亚洲精品久久久久秋霞| av大片在线无码免费| 黄色三级三级三级免费看| 久久精品国产亚洲av高清漫画| 国产又长又粗又爽免费视频| a级午夜毛片免费一区二区| 亚洲人成电影网站|