标签:通过 就是 依赖性 group 一个 异步 64位系统 安装 存储
发送者将消息发送给消息服务器,消息服务器将消感存放在若千队列中,在合适的时候再将消息转发给接收者。
这种模式下,发送和接收是异步的,发送者无需等待; 二者的生命周期未必相同: 发送消息的时候接收者不一定运行,接收消息的时候发送者也不一定运行;一对多通信: 对于一个消息可以有多个接收者。
JMS是java的消息服务,JMS的客户端之间可以通过JMS服务进行异步的消息传输。
里面涉及到的概念以及特点
1.主题(Topic)
2.发布者(Publisher)
3.订阅者(Subscriber)
客户端将消息发送到主题。多个发布者将消息发送到Topic,系统将这些消息传递给多个订阅者。
Pub/Sub的特点
1.每个消息可以有多个消费者
2.发布者和订阅者之间有时间上的依赖性。针对某个主题(Topic)的订阅者,它必须创建一个订阅者之后,才能消费发布者的消息,而且为了消费消息,订阅者必须保持运行的状态。
用户注册、订单修改库存、日志存储
解压当前activeMQ
进入bin目录下启动64位系统下的
启动成功访问页面:
引入pom文件依赖
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
</dependencies>
生产者代码
public static void main(String[] args) throws JMSException {
//连接工厂JMS 用它创建连接
ConnectionFactory collectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
Connection createConnection = collectionFactory.createConnection();
createConnection.start();
// Session: 一个发送或接收消息的线程
Session session = createConnection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
// Destination :消息的目的地;消息发送给谁.
// 获取session注意参数值xiaobai是Query的名字
Destination destination = session.createQueue("xiaobai");
//MessageProducer:消息生产者
MessageProducer producer = session.createProducer(destination);
//设置持久化
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
for (int i = 0; i < 5; i++) {
System.out.println("我是生产者" + i);
sendMsg(session, producer, "我是生产者" + i);
session.commit();
}
System.out.println("我是生产者发送完毕" );
}
public static void sendMsg(Session session, MessageProducer producer, String i) throws JMSException {
TextMessage textMessage = session.createTextMessage("hello activemq" + i);
producer.send(textMessage);
}
消费者代码:
//连接工厂,JMS 用它创建连接
ConnectionFactory collectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
Connection createConnection = collectionFactory.createConnection();
createConnection.start();
//Session: 一个发送或接收消息的线程
Session session = createConnection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("xiaobai");
MessageConsumer createConsumer = session.createConsumer(destination);
while (true) {
TextMessage textMessage = (TextMessage) createConsumer.receive();
if (textMessage != null) {
String text = textMessage.getText();
System.out.println(text);
// textMessage.acknowledge();
session.commit();
} else {
break;
}
System.out.println("消费者消费完毕");
}
启动生产者,在消息中间查看数据
启动消费者,查看消费情况
生产者代码:
ConnectionFactory collectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
Connection createConnection = collectionFactory.createConnection();
createConnection.start();
Session session = createConnection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(null);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
for (int i = 0; i < 5; i++) {
System.out.println("我是生产者" + i);
sendMsg(session, producer, "我是生产者" + i);
}
System.out.println("我是生产者发送完毕" );
}
public static void sendMsg(Session session, MessageProducer producer, String i) throws JMSException {
TextMessage textMessage = session.createTextMessage("hello activemq" + i);
Destination destination = session.createTopic("xiao_topic");
producer.send(destination,textMessage);
}
消费者代码
ConnectionFactory collectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
Connection createConnection = collectionFactory.createConnection();
createConnection.start();
Session session = createConnection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createTopic("xiao_topic");
MessageConsumer createConsumer = session.createConsumer(destination);
while (true) {
TextMessage textMessage = (TextMessage) createConsumer.receive();
if (textMessage != null) {
String text = textMessage.getText();
System.out.println(text);
} else {
break;
}
System.out.println("消费者消费完毕");
}
}
先启动的消费者,后启动生产者
标签:通过 就是 依赖性 group 一个 异步 64位系统 安装 存储
原文地址:https://www.cnblogs.com/Libbo/p/11546816.html