<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
    數(shù)據(jù)加載中……

    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>

    然后使用如下的代碼發(fā)送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);
        }

    發(fā)送消息是沒有問題的,但是當密集發(fā)送大量消息時,會拋出地址占用,無法創(chuàng)建connection(或者在某些較老版本下,當消息數(shù)達到65535條的時候,也會無法再次發(fā)送,只能重啟隊列),具體異常如下
    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

    無法發(fā)送的真正原因就在這里了
    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.

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

    至于解決的方法,第二段話也說的很清楚,要不就使用EJB容器的ConnectionFactory,要么就使用ActiveMQ提供的PooledConnectionFactory,這個類其實也是實現(xiàn)了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并發(fā)發(fā)送/接收消息

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


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


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


    網(wǎng)站導航:
     
    主站蜘蛛池模板: 亚洲黄色高清视频| 亚洲最新黄色网址| 亚洲国产av高清无码| 色窝窝亚洲av网| 男人的天堂网免费网站| 卡1卡2卡3卡4卡5免费视频| 国产亚洲情侣一区二区无码AV| 精品亚洲aⅴ在线观看| 亚洲6080yy久久无码产自国产| 中文在线观看永久免费| 久久不见久久见免费影院| 亚洲精品偷拍视频免费观看| 亚洲精品456在线播放| 国产亚洲视频在线播放大全| 1区2区3区产品乱码免费| 亚洲欧洲久久av| 国产成人精品日本亚洲专一区| 精品多毛少妇人妻AV免费久久| 美女裸身网站免费看免费网站| 亚洲一区爱区精品无码| 亚洲爆乳AAA无码专区| 毛片无码免费无码播放 | 亚洲AV无码一区二区乱孑伦AS | 亚洲AV一二三区成人影片| 午夜在线免费视频 | 337p日本欧洲亚洲大胆色噜噜 | 久久精品国产亚洲AV久| 少妇无码一区二区三区免费| 狠狠亚洲婷婷综合色香五月排名 | av片在线观看永久免费| 国产yw855.c免费视频| 亚洲欧美综合精品成人导航| 久久国内免费视频| 亚洲国产电影在线观看| 2022久久国产精品免费热麻豆| 亚洲国产日韩一区高清在线| 国产一区二区三区免费| 亚洲码国产精品高潮在线| GOGOGO高清免费看韩国| 亚洲自偷自偷在线制服| 中文字幕的电影免费网站|