订阅即多消费者对同一topic的订阅,生产消息将被所订阅的所有消费者接收。
生产者生成的消息只会被订阅的消费者消费,如若消费者宕机,在宕机过程中产生的消息不再被恢复的消费者消费。
生产者
package com.ccycc.activemq.project.subscriber.producer;
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 org.apache.activemq.ActiveMQConnectionFactory;
public class JMSProducer {
public static void main(String[] args) {
JMSProducer jmsProducer = new JMSProducer();
jmsProducer.producer();
}
private void producer() {
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
Destination destination = null;
MessageProducer producer = null;
try {
connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.182.129:61616");
//创建连接
connection = connectionFactory.createConnection();
//启动连接
connection.start();
//创建session
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
//创建消息队列
destination = session.createTopic("jms_t");
//对于该队列创建生成者
producer = session.createProducer(destination);
//生成者发生消息
producer.send(session.createTextMessage("hello"));
session.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
if(session != null) {
session.close();
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(connection != null) {
connection.close();
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
消费者
package com.ccycc.activemq.project.subscriber.consumer;
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.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JMSConsumer {
public static void main(String[] args) {
JMSConsumer consumer = new JMSConsumer();
consumer.consumer();
}
public void consumer() {
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
Destination destination = null;
MessageConsumer consumer = null;
try {
connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.182.129:61616");
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createTopic("jms_t");
consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
if(message != null) {
if(message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
//textMessage.getJMSRedelivered()
System.out.println(textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
可订阅历史消息
生产者
package com.ccycc.activemq.project.subscriber.producer;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JMSProducer {
public static void main(String[] args) {
JMSProducer jmsProducer = new JMSProducer();
jmsProducer.producer();
}
private void producer() {
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
Destination destination = null;
MessageProducer producer = null;
try {
connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.182.129:61616");
//创建连接
connection = connectionFactory.createConnection();
//创建session
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
//创建消息队列
destination = session.createTopic("jms_t");
//对于该队列创建生成者
producer = session.createProducer(destination);
//设置为持久化订阅方式
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
//设置完成启动连接
connection.start();
//生成者发生消息
producer.send(session.createTextMessage("hello"));
session.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
if(session != null) {
session.close();
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(connection != null) {
connection.close();
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
消费者
package com.ccycc.activemq.project.subscriber.consumer;
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.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JMSConsumer {
public static void main(String[] args) {
JMSConsumer consumer = new JMSConsumer();
consumer.consumer();
}
public void consumer() {
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
Destination destination = null;
MessageConsumer consumer = null;
String clientId = "id";
try {
connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.182.129:61616");
connection = connectionFactory.createConnection();
connection.setClientID(clientId);
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("jms_t");
consumer = session.createDurableSubscriber(topic, "sub");
//consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
if(message != null) {
if(message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
//textMessage.getJMSRedelivered()
System.out.println(textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});
connection.start();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}