队列模式
1.生产者代码:
package com.zzf.jms.queue;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class AppProducer {
private static final String url="tcp://192.168.1.6:61616";
private static final String queueName="queue-text";
public static void main(String[] args) throws JMSException {
//1.创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
//((ActiveMQConnectionFactory) connectionFactory).setUserName("admin");
//((ActiveMQConnectionFactory) connectionFactory).setPassword("admin");
//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 producer = session.createProducer(destination);
for (int i = 0; i <10 ; i++) {
//7.创建消息
TextMessage textMessage=session.createTextMessage("消息内容"+i);
//8.发送消息
producer.send(textMessage);
//
System.out.println(textMessage.getText());
}
//9.关闭连接
connection.close();
}
}
上文的url可以改成“tcp://localhost:61616”,或者通过cmd->ipconfig
2.消费者代码:
package com.zzf.jms.queue;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class AppConsumer {
private static final String url="tcp://192.168.1.6:61616";
private static final String queueName="queue-text";
public static void main(String[] args) throws JMSException {
//1.创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
//((ActiveMQConnectionFactory) connectionFactory).setUserName("admin");
//((ActiveMQConnectionFactory) connectionFactory).setPassword("admin");
//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.创建一个消费者
MessageConsumer consumer = session.createConsumer(destination);
//7.创建一个监听器
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("接受消息:"+textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
//8.关闭连接
// connection.close();
}
}
输出(启动消费者模式,再启动生产者)
(队列模式如上)
主题模式
1.发布者代码:
package com.zzf.jms.topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class AppProducer {
private static final String url="tcp://192.168.1.6:61616";
private static final String topicName="topic-text";
public static void main(String[] args) throws JMSException {
//1.创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
//((ActiveMQConnectionFactory) connectionFactory).setUserName("admin");
//((ActiveMQConnectionFactory) connectionFactory).setPassword("admin");
//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 producer = session.createProducer(destination);
for (int i = 0; i <10 ; i++) {
//7.创建消息
TextMessage textMessage = session.createTextMessage("消息内容" + i);
//8.发送消息
producer.send(textMessage);
//
System.out.println(textMessage.getText());
}
//9.关闭连接
connection.close();
}
}
2.接受者代码:
package com.zzf.jms.topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class AppConsumer {
private static final String url="tcp://192.168.1.6:61616";
private static final String topicName="topic-text";
public static void main(String[] args) throws JMSException {
//1.创建ConnectionFactory
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
//((ActiveMQConnectionFactory) connectionFactory).setUserName("admin");
//((ActiveMQConnectionFactory) connectionFactory).setPassword("admin");
//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.创建一个消费者
MessageConsumer consumer = session.createConsumer(destination);
//7.创建一个监听器
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("接受消息:"+textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
//8.关闭连接
// connection.close();
}
}
唯一需要改的地方是session.createTopic.
和队列模式一样,先启动接受者(启动一个以上),再启动发布者
每个接受者都接受到了发布者的发布信息(公众号)
注意:需要导入jar包:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>