ActiveMQ
版本:5.15.11
1 JMS
原始API
操作ActiveMQ
1.1 queue
点对点模式
ActiveMqConfig.java
:
package com.tao.springbootdemo.mq.activemq;
import org.apache.activemq.ActiveMQConnection;
/**
* activemq的配置
*/
public class ActiveMqConfig {
public static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
public static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
public static final String BROKER_URL = "tcp://192.168.48.128:61616";
}
QueueProducer.java
生产者:
package com.tao.springbootdemo.mq.activemq.jms.queue;
import com.tao.springbootdemo.mq.activemq.ActiveMqConfig;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jms.*;
/**
* queue消息生产者
*/
public class QueueProducer {
private static final Logger LOG = LoggerFactory.getLogger(QueueProducer.class);
private Connection connection;
private Session session;
private MessageProducer producer;
public QueueProducer(String queueName) {
init(queueName);
}
/**
* 初始化
*/
public void init(String queueName) {
try {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMqConfig.BROKER_URL);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(queueName);
producer = session.createProducer(queue);
} catch (JMSException e) {
LOG.error("" + e);
}
}
/**
* 发送消息
*/
public void sendMsg(String message) {
try {
ActiveMQTextMessage msg = new ActiveMQTextMessage();
msg.setText(message);
producer.send(msg);
session.commit();
LOG.info(Thread.currentThread().getName() + " send a message: {}", msg.getText());
} catch (JMSException e) {
LOG.error("" + e);
}
}
}
QueueConsumer.java
消费者:
package com.tao.springbootdemo.mq.activemq.jms.queue;
import com.tao.springbootdemo.mq.activemq.ActiveMqConfig;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jms.*;
/**
* queue消息消费者
*/
public class QueueConsumer {
private static final Logger LOG = LoggerFactory.getLogger(QueueConsumer.class);
private Connection connection;
private Session session;
private MessageConsumer consumer;
public QueueConsumer(String queueName) {
init(queueName);
}
/**
* 初始化
*/
public void init(String queueName) {
try {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMqConfig.BROKER_URL);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(queueName);
consumer = session.createConsumer(queue);
} catch (JMSException e) {
LOG.error("" + e);
}
}
/**
* 接收消息
*/
public void receiveMsg() {
try {
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage) message;
LOG.info(Thread.currentThread().getName() + " receive a message : {}", textMessage.getText());
} catch (JMSException e) {
LOG.error("" + e);
}
}
});
} catch (JMSException e) {
LOG.error("" + e);
}
}