activemq使用PoolConnectionFactory中的問題與activemq jar版本問題

最近使用activemq來替代UDP進行消息的發送與接收,在整改項目中,踩了很多坑,爲了給以後留下經驗,總結了使用中遇到的問題。1 、 activemq的安裝也遇到了一些問題,下次總結,總之流程時,先在官網上下載activemq在本地,根據電腦系統選擇bin下對於的文件夾,點擊activemq.bat批處理文件進行安裝。安裝完成後在瀏覽器打開 http://localhost:8161/admin 輸入用戶名和密碼;默認都是admin,進入activemq管理器,其中有個queue點擊進去,這裏將會展示你所創建的隊列信息。如圖:


mqQueue是我創建的隊列名稱,裏面有5條消息。暫時沒有消費者消費。

2、在使用jms自己連接池時,這裏沒有使用spring的JMS模板,也無需配置

springapplication-activemq.xml文件,代碼如下:

import javax.jms.Connection;

import javax.jms.DeliveryMode;

import javax.jms.Destination;

import javax.jms.JMSException;

import javax.jms.MessageConsumer;

import javax.jms.MessageProducer;

import javax.jms.Session;

import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

import org.apache.activemq.pool.PooledConnectionFactory;

import org.apache.log4j.Logger;

public class MessageController {

private static Logger logger = Logger.getLogger(MessageController.class);

// Destination :消息的目的地;消息發送給誰. 

private static Destination destination; 

//隊列消息生產者 //

 private static MQProducer producer; 

//隊列消息消費者 

private MessageConsumer consumer; 

private Connection connection = null; 

// Session: 一個發送或接收消息的線程 Session session;

// MessageProducer:消息發送者

private MessageProducer producer;

// 構造ConnectionFactory實例對象,此處採用ActiveMq的實現jarprivate ActiveMQConnectionFactory connectionFactory;/*生產者發送消息*/ 

public Boolean sendMessage(String message,String host_name){ 

Boolean flag=false; 

String host_name1="tcp://localhost:61616"; 

connectionFactory = new ActiveMQConnectionFactory("admin","admin", host_name1);

// Connection :JMS 客戶端到JMS Provider 的連接標準接口 

// 注意PooledConnectionFactory 引入的jar包與ActiveMQConnectionFactory 引入的jar包要爲同一個, 

// 即引入的jar包中必須同時包含PooledConnectionFactory 和 ActiveMQConnectionFactory ,原因在於後後面將兩者實例對 //象進行了合併,不然就會一直報錯。 

PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory(); 

pooledConnectionFactory.setConnectionFactory(connectionFactory); 

  try { 

// 構造從工廠得到連接對象 

    connection=pooledConnectionFactory.createConnection(); 

// 獲取操作連接 

    session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);

// 獲取session注意參數值是一個服務器的queue,須在在ActiveMq的console配置 

    logger.debug(session.createQueue("mqQueue"));

     destination = session.createQueue("mqQueue"); 

// 得到消息生成者【發送者】

     producer = session.createProducer(destination); 

// 設置不持久化 producer.setDeliveryMode(DeliveryMode.PERSISTENT); 

    String message1="測試消息對列111"; 

        for (int i = 1; i <=5 ; i++) { 

            TextMessage msg = session.createTextMessage(message1); 

            // 發送消息到目的地方 

            System.out.println("發送消息:" + i); 

              producer.send(msg); 

                flag=true; 

        } session.commit(); 

     } catch (Exception e) { 

        e.printStackTrace(); 

            flag=false; 

      }finally { 

            if (null != connection){

                 try { 

                        connection.close();

                 }catch (JMSException e) {

                     e.printStackTrace(); 

                   }

             }

 } 

return flag; 

}

3、 當使用activemq-all-5.14.jar時需添加commons-pool2的jar包,但是activemq-all-5.14.jar這個jar包可能會與spring的相關jar包產生衝突,就遇到了衝突,但是又不能將activemq-all-5.14.jar中依賴的jar包單獨使用,

原因是這句代碼:pooledConnectionFactory.setConnectionFactory(connectionFactory);

它將ActiveMQConnectionFactory實例化的對象作爲pooledConnectionFactory的屬性值,

而activemq-all-5.14.1jar所依賴的包中沒有將兩者結合在一起的jar包,單獨添加jar包會使類型不匹配。

使用時候要注意了。在本地測試通過,控制檯打印出以下信息:

2017-06-06 22:08:51:493 main org.apache.activemq.util.IdGenerator 67 Using port 0 

2017-06-06 22:08:51:617 main org.apache.activemq.transport.AbstractInactivityMonitor 407 Starting connection check task for: tcp://localhost:61616 

2017-06-06 22:08:51:685 main org.apache.activemq.transport.WireFormatNegotiator 82 Sending: WireFormatInfo { version=12, properties={CacheSize=1024, MaxFrameSize=9223372036854775807, CacheEnabled=true, Host=localhost, ProviderName=ActiveMQ, SizePrefixDisabled=false, MaxInactivityDurationInitalDelay=10000, TightEncodingEnabled=true, StackTraceEnabled=true, ProviderVersion=5.14.0, PlatformDetails=JVM: 1.7.0_51, 24.51-b03, Oracle Corporation, OS: Windows 8, 6.2, x86, TcpNoDelayEnabled=true, MaxInactivityDuration=30000}, magic=[A,c,t,i,v,e,M,Q]}

 2017-06-06 22:08:51:685 ActiveMQ Transport: tcp://localhost/127.0.0.1:61616@64552 org.apache.activemq.transport.tcp.TcpTransport 211 TCP consumer thread for tcp://localhost/127.0.0.1:61616@64552 starting 

2017-06-06 22:08:51:700 ActiveMQ Transport: tcp://localhost/127.0.0.1:61616@64552 org.apache.activemq.transport.AbstractInactivityMonitor 431 Stopping connection check task for: tcp://localhost/127.0.0.1:61616@64552 2017-06-06 22:08:51:700 main org.apache.activemq.jms.pool.PooledConnectionFactory 108 Created new connection: ConnectionPool[ActiveMQConnection {id=ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1,clientId=null,started=false}] 

2017-06-06 22:08:51:716 ActiveMQ Transport: tcp://localhost/127.0.0.1:61616@64552 org.apache.activemq.transport.InactivityMonitor 103 Using min of local: WireFormatInfo { version=12, properties={CacheSize=1024, MaxFrameSize=9223372036854775807, CacheEnabled=true, Host=localhost, ProviderName=ActiveMQ, SizePrefixDisabled=false, MaxInactivityDurationInitalDelay=10000, TightEncodingEnabled=true, StackTraceEnabled=true, ProviderVersion=5.14.0, PlatformDetails=JVM: 1.7.0_51, 24.51-b03, Oracle Corporation, OS: Windows 8, 6.2, x86, TcpNoDelayEnabled=true, MaxInactivityDuration=30000}, magic=[A,c,t,i,v,e,M,Q]} and remote: WireFormatInfo { version=12, properties={CacheSize=1024, MaxFrameSize=104857600, ProviderVersion=5.14.0, CacheEnabled=true, ProviderName=ActiveMQ, SizePrefixDisabled=false, TcpNoDelayEnabled=true, MaxInactivityDurationInitalDelay=10000, PlatformDetails=JVM: 1.7.0_51, 24.51-b03, Oracle Corporation, OS: Windows 8, 6.2, x86, MaxInactivityDuration=30000, TightEncodingEnabled=true, StackTraceEnabled=true}, magic=[A,c,t,i,v,e,M,Q]} 

2017-06-06 22:08:51:716 ActiveMQ Transport: tcp://localhost/127.0.0.1:61616@64552 org.apache.activemq.transport.WireFormatNegotiator 130 Received WireFormat: WireFormatInfo { version=12, properties={CacheSize=1024, MaxFrameSize=104857600, ProviderVersion=5.14.0, CacheEnabled=true, ProviderName=ActiveMQ, SizePrefixDisabled=false, TcpNoDelayEnabled=true, MaxInactivityDurationInitalDelay=10000, PlatformDetails=JVM: 1.7.0_51, 24.51-b03, Oracle Corporation, OS: Windows 8, 6.2, x86, MaxInactivityDuration=30000, TightEncodingEnabled=true, StackTraceEnabled=true}, magic=[A,c,t,i,v,e,M,Q]} 

2017-06-06 22:08:51:716 ActiveMQ Transport: tcp://localhost/127.0.0.1:61616@64552 org.apache.activemq.transport.WireFormatNegotiator 137 tcp://localhost/127.0.0.1:61616@64552 before negotiation: OpenWireFormat{version=12, cacheEnabled=false, stackTraceEnabled=false, tightEncodingEnabled=false, sizePrefixDisabled=false, maxFrameSize=9223372036854775807} 

2017-06-06 22:08:51:732 ActiveMQ Transport: tcp://localhost/127.0.0.1:61616@64552 org.apache.activemq.transport.WireFormatNegotiator 152 tcp://localhost/127.0.0.1:61616@64552 after negotiation: OpenWireFormat{version=12, cacheEnabled=true, stackTraceEnabled=true, tightEncodingEnabled=true, sizePrefixDisabled=false, maxFrameSize=104857600} 

2017-06-06 22:08:52:040 main com.lactec.crmConfiguration.util.MessageController 65 queue://mqQueue yeah 2017/6/6 23:06:47 發送消息:12017-06-06 22:08:52:120 main org.apache.activemq.TransactionContext 250 Begin:TX:ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1 

2017-06-06 22:08:52:162 main org.apache.activemq.ActiveMQSession 1948 ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1 sending message: ActiveMQTextMessage {commandId = 0, responseRequired = false, messageId = ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1:1, destination = queue://mqQueue, transactionId = TX:ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1, expiration = 0, timestamp = 1496758132121, arrival = 0, brokerInTime = 0, brokerOutTime = 0, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false, text = 測試消息對列111} 發送消息:2

2017-06-06 22:08:52:162 main org.apache.activemq.ActiveMQSession 1948 ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1 sending message: ActiveMQTextMessage {commandId = 0, responseRequired = false, messageId = ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1:1:2, originalDestination = null, originalTransactionId = null, producerId = ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1:1, destination = queue://mqQueue, transactionId = TX:ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1, expiration = 0, timestamp = 1496758132162, arrival = 0, brokerInTime = 0, brokerOutTime = 0, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false, text = 測試消息對列111} 發送消息:3yeah 

2017/6/6 23:07:002017-06-06 22:08:52:162 main org.apache.activemq.ActiveMQSession 1948 ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1 sending message: ActiveMQTextMessage {commandId = 0, responseRequired = false, messageId = ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1:1:3, originalDestination = null, originalTransactionId = null, producerId = ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1:1, destination = queue://mqQueue, transactionId = TX:ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1, expiration = 0, timestamp = 1496758132162, arrival = 0, brokerInTime = 0, brokerOutTime = 0, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false, text = 測試消息對列111} 發送消息:4

2017-06-06 22:08:52:178 main org.apache.activemq.ActiveMQSession 1948 ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1 sending message: ActiveMQTextMessage {commandId = 0, responseRequired = false, messageId = ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1:1:4, originalDestination = null, originalTransactionId = null, producerId = ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1:1, destination = queue://mqQueue, transactionId = TX:ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1, expiration = 0, timestamp = 1496758132162, arrival = 0, brokerInTime = 0, brokerOutTime = 0, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false, text = 測試消息對列111} 發送消息:5

2017-06-06 22:08:52:209 main org.apache.activemq.ActiveMQSession 1948 ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1 sending message: ActiveMQTextMessage {commandId = 0, responseRequired = false, messageId = ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1:1:5, originalDestination = null, originalTransactionId = null, producerId = ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1:1, destination = queue://mqQueue, transactionId = TX:ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1, expiration = 0, timestamp = 1496758132209, arrival = 0, brokerInTime = 0, brokerOutTime = 0, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false, text = 測試消息對列111} 

2017-06-06 22:08:52:209 main org.apache.activemq.ActiveMQSession 580 ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1 Transaction Commit :TX:ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1 

2017-06-06 22:08:52:209 main org.apache.activemq.TransactionContext 323 Commit: TX:ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1 syncCount: 0 

2017-06-06 22:08:52:336 main org.apache.activemq.ActiveMQSession 601 ID:DESKTOP-KUJ07OM-64551-1496758131493-1:1:1 Transaction Rollback, txid:null

4、使用activemq-4.0-m3時,需要添加commons-pool的jar包,這是java5.0以上使用的。在使用上述代碼來發送消息時,可能會遇到以下錯誤:ActiveMQConnection 1292 Async exception with no exception listener ,解決辦法將在下一篇文章給出。筆者初次接觸activemq,能力有限,如有不足指出,還望指正,多謝!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章