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