一、点对点通信
1、消息发送者
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 ProducerTest {
private static final int SENDNUM = 10;
public static void main(String[] args) throws Exception {
ConnectionFactory connectionFactory;//连接工程
Connection connection = null;//连接
Session session;//会话 结束或签字发送消息的线程
Destination destination;//消息的目的地
MessageProducer messageProducer;//消息生产者
try {
//实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://ip:61616");
//通过连接工程获取连接
connection = connectionFactory.createConnection();
connection.start();//启动连接
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);//创建session
destination = session.createQueue("FirstQueue1");//创建队列
messageProducer = session.createProducer(destination);//创建消息生产者
sendMessage(session, messageProducer);
session.commit();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(connection != null)
connection.close();
}
}
public static void sendMessage(Session session,MessageProducer messageProducer) throws Exception{
for (int i = 0; i < SENDNUM; i++) {
TextMessage message = session.createTextMessage("ActiveMq 发送消息"+i);
messageProducer.send(message);
}
}
}
2、消费者
(1)非监听器模式
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 消息消费者
* @author Administrator
*
*/
public class JMSConsumer {
private static final String USERNAME="admin"; // 默认的连接用户名
private static final String PASSWORD="admin"; // 默认的连接密码
private static final String BROKEURL = "tcp://ip:61616";
public static void main(String[] args) {
ConnectionFactory connectionFactory; // 连接工厂
Connection connection = null; // 连接
Session session; // 会话 接受或者发送消息的线程
Destination destination; // 消息的目的地
MessageConsumer messageConsumer; // 消息的消费者
// 实例化连接工厂
connectionFactory=new ActiveMQConnectionFactory(JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKEURL);
try {
connection=connectionFactory.createConnection(); // 通过连接工厂获取连接
connection.start(); // 启动连接
session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); // 创建Session
destination=session.createQueue("FirstQueue1"); // 创建连接的消息队列
messageConsumer=session.createConsumer(destination); // 创建消息消费者
while(true){
TextMessage textMessage=(TextMessage)messageConsumer.receive(100000);
if(textMessage!=null){
System.out.println("收到的消息:"+textMessage.getText());
}else{
break;
}
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
(2)、监听器模式,消费者会实时消息监听,若有消息则立即消费
a、监听器:
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
/**
* 消息监听
* @author Administrator
*
*/
public class Listener implements MessageListener{
@Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
try {
System.out.println("收到的消息:"+((TextMessage)message).getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
b、消费者
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 消息消费者
* @author Administrator
*
*/
public class JMSConsumer2 {
private static final String USERNAME="admin"; // 默认的连接用户名
private static final String PASSWORD="admin"; // 默认的连接密码
private static final String BROKEURL="tcp://ip:61616"; // 默认的连接地址
public static void main(String[] args) {
ConnectionFactory connectionFactory; // 连接工厂
Connection connection = null; // 连接
Session session; // 会话 接受或者发送消息的线程
Destination destination; // 消息的目的地
MessageConsumer messageConsumer; // 消息的消费者
// 实例化连接工厂
connectionFactory=new ActiveMQConnectionFactory(JMSConsumer2.USERNAME, JMSConsumer2.PASSWORD, JMSConsumer2.BROKEURL);
try {
connection=connectionFactory.createConnection(); // 通过连接工厂获取连接
connection.start(); // 启动连接
session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); // 创建Session
destination=session.createQueue("FirstQueue1"); // 创建连接的消息队列
messageConsumer=session.createConsumer(destination); // 创建消息消费者
messageConsumer.setMessageListener(new Listener()); // 注册消息监听
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
二、订阅模式
一个发布多个订阅者都会受到消息,先订阅后发布
1、消息生产者
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 javax.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ProducerTest2 {
private static final int SENDNUM = 10;
public static void main(String[] args) throws Exception {
ConnectionFactory connectionFactory;//连接工程
Connection connection = null;//连接
Session session;//会话 结束或签字发送消息的线程
Topic createTopic;//消息的目的地
MessageProducer messageProducer;//消息生产者
try {
//实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://ip:61616");
//通过连接工程获取连接
connection = connectionFactory.createConnection();
connection.start();//启动连接
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);//创建session
createTopic = session.createTopic("FirstTopic1");
messageProducer = session.createProducer(createTopic);//创建消息生产者
sendMessage(session, messageProducer);
session.commit();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(connection != null)
connection.close();
}
}
public static void sendMessage(Session session,MessageProducer messageProducer) throws Exception{
for (int i = 0; i < 1; i++) {
TextMessage message = session.createTextMessage("ActiveMq 发送消息"+i);
messageProducer.send(message);
}
}
}
2、消费者(这边就采用监听器模式)
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 消息消费者,订阅模式,一个发布多个订阅者都会受到消息,先订阅后发布
* @author Administrator
*
*/
public class JMSConsumer2 {
private static final String USERNAME="admin"; // 默认的连接用户名
private static final String PASSWORD="admin"; // 默认的连接密码
private static final String BROKEURL="tcp://ip:61616"; // 默认的连接地址
public static void main(String[] args) {
ConnectionFactory connectionFactory; // 连接工厂
Connection connection = null; // 连接
Session session; // 会话 接受或者发送消息的线程
Destination destination; // 消息的目的地
MessageConsumer messageConsumer; // 消息的消费者
// 实例化连接工厂
connectionFactory=new ActiveMQConnectionFactory(JMSConsumer2.USERNAME, JMSConsumer2.PASSWORD, JMSConsumer2.BROKEURL);
try {
connection=connectionFactory.createConnection(); // 通过连接工厂获取连接
connection.start(); // 启动连接
session=connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); // 创建Session
destination=session.createTopic("FirstTopic1");
messageConsumer=session.createConsumer(destination); // 创建消息消费者
messageConsumer.setMessageListener(new Listener()); // 注册消息监听
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
订阅模式需要先订阅才能消费到信息,也就是若先启动生产者进行生产消息,在用消费者是无法接收到信息,要先使用消费者订阅完,再使用生产者才行
生产者发布一个消息,能够被多个订阅的消费者接收到