ActiveMQ消息模式主要是队列模式和主题模式。
队列模式
“`消费者
AppConsumer.javapackage come.jms.queue;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
//队列模式
public class AppConsumer {
// 默认端口 61616
private static final String url = “tcp://127.0.0.1:61616”;
private static final String queueName = “queue-test”;
public static void main(String[] args) throws JMSException {
// 1创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
// 2创建 connection
Connection connection = connectionFactory.createConnection();
// 3启动连接
connection.start();
// 4创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 5创建目标
Destination destination = session.createQueue(queueName);
// 创建 消费者
MessageConsumer messageConsumer = session.createConsumer(destination);
// 创建 监听者
messageConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("接受的消息:" + textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
// 关闭连接 注意 连接 需要在异步完成后在关闭
//connection.close();
}
}
> ``` 队列生产者
> AppProducer.java
>
> package come.jms.queue;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
//队列模式
public class AppProducer {
// 默认端口 61616
private static final String url = "tcp://127.0.0.1:61616";
private static final String queueName = "queue-test";
public static void main(String[] args) throws JMSException {
// 1创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
// 2创建 connection
Connection connection = connectionFactory.createConnection();
// 3启动连接
connection.start();
// 4创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 5创建目标
Destination destination = session.createQueue(queueName);
// 6 创建生产者
MessageProducer messageProducer = session.createProducer(destination);
for (int i = 0; i < 50; i++) {
// 创建消息
TextMessage textMessage = session.createTextMessage("message:" + i);
// 发送消息
messageProducer.send(textMessage);
System.err.println(textMessage);
}
System.err.println("over");
// 关闭连接
connection.close();
}
}
**主题(topic)模式**
package come.jms.topic;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
//主题模式
public class AppConsumer {
// 默认端口 61616
private static final String url = “tcp://127.0.0.1:61616”;
private static final String topicName = “topic-test”;
public static void main(String[] args) throws JMSException {
// 1创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
// 2创建 connection
Connection connection = connectionFactory.createConnection();
// 3启动连接
connection.start();
// 4创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 5创建目标
Destination destination = session.createTopic(topicName);
// 创建 消费者
MessageConsumer messageConsumer = session.createConsumer(destination);
// 创建 监听者
messageConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("接受的消息:" + textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
// 关闭连接 注意 连接 需要在异步完成后在关闭
//connection.close();
}
}
package come.jms.topic;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
//主题模式
public class AppConsumer {
// 默认端口 61616
private static final String url = “tcp://127.0.0.1:61616”;
private static final String topicName = “topic-test”;
public static void main(String[] args) throws JMSException {
// 1创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
// 2创建 connection
Connection connection = connectionFactory.createConnection();
// 3启动连接
connection.start();
// 4创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 5创建目标
Destination destination = session.createTopic(topicName);
// 创建 消费者
MessageConsumer messageConsumer = session.createConsumer(destination);
// 创建 监听者
messageConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("接受的消息:" + textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
// 关闭连接 注意 连接 需要在异步完成后在关闭
//connection.close();
}
}
AppProducer.java
package come.jms.topic;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
// 主题模式 只能接受监听后发布的消息
public class AppProducer {
//默认端口 61616
private static final String url = “tcp://127.0.0.1:61616”;
private static final String topicName = “topic-test”;
public static void main(String[] args) throws JMSException {
// 1创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
// 2创建 connection
Connection connection = connectionFactory.createConnection();
// 3启动连接
connection.start();
// 4创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//5创建目标
Destination destination=session.createTopic(topicName);
//6 创建生产者
MessageProducer messageProducer=session.createProducer(destination);
for (int i = 0; i < 50; i++) {
// 创建消息
TextMessage textMessage=session.createTextMessage("message:"+i);
// 发送消息
messageProducer.send(textMessage);
System.err.println(textMessage);
}
System.err.println("over");
// 关闭连接
connection.close();
}
}
“`