队列模式
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>
今天的文章AMQ初级使用(队列模式+主题模式)分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/6564.html