http://activemq.apache.org/官网下载
在bin包下启动服务器。提供了图形化页面,默认localhost:8161
程序中需要导的包的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
消息队列的特点:削峰,异步,解耦
消息队列两种模式:
点到点模式:生产者发送一个消息,而同时有多个消费者,只有一个消费者能得到消息
发布订阅模式:类似于广播一样,只有消费者开启时,才能接受到消息,如果当时没打开消费者,就接受不到了,同时多个消费者都能得到消息。
生产者:
public class MyProducer {
private static final String ACTIVEMQ_URL="tcp://localhost:61616";
public static void main(String[] args) throws JMSException {
//创建连接工厂
ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);
//创建连接
Connection connection = activeMQConnectionFactory.createConnection();
//打开连接
connection.start();
//创建会话
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
//创建队列
Destination destination = session.createQueue("MyQueue");
//创建一个生产者
MessageProducer messageProducer=session.createProducer(destination);
//向队列推送那个10个文本消息
for (int i=0;i<10;i++){
//创建文本消息
TextMessage textMessage = session.createTextMessage("第"+i+"个消息");
messageProducer.send(textMessage);
System.out.println("已发送消息"+textMessage.getText());
}
connection.close();
}
}
生产者生产了10条消息,放在了MyQueue队列中
,
消费者消费
消息消费者是由会话创建的一个对象,它用于接收发送到目的地的消息。消息的消费可以采用以下两种方法之一:
同步消费。通过调用消费者的receive方法从目的地中显式提取消息。receive方法可以一直阻塞到消息到达。
异步消费。客户可以为消费者注册一个消息监听器,以定义在消息到达时所采取的动作
public class MyCustomer {
public static void main(String[] args) throws JMSException {
ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory();
Connection connection = activeMQConnectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("MyQueue");
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
TextMessage testmessage= (TextMessage) message;
try {
System.out.println("消费的消息" + testmessage);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
public class Receiver {
public static void main(String[] args) throws JMSException {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,ActiveMQConnectionFactory.DEFAULT_PASSWORD,ActiveMQConnectionFactory.DEFAULT_BROKER_URL);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);//不开启事务,自动确认机制
Queue queue = session.createQueue("user");
MessageConsumer consumer = session.createConsumer(queue);
while (true) {
TextMessage receive = (TextMessage) consumer.receive();
System.out.println(receive.getText());
}
}
}
什么是JMS
javaMessageService
ConnectionFctory:创建Connection对象的工厂,针对两种不同的jms消息模型,分别是QueueConnectionFactory和TopicConnectionFactory
Connection:表是客户端和JMS系统之间的建立的连接,Connection可以产生一个或者多个session。connection也有两种类型:QueueConnection和TopicConnection
Session:Session是我们操作消息的接,可以通过session创建生产者,消费者,消息等,session提供了事务的功能,当我们需要使用session发送或接受多个消息的是后,可以将接受或发送动作发在一个事务中,分别是QueueSession和TopicSession
Destination:消息目的地,对于生产者来说他的Destination是某个队列或某个主题Topic,对于消费者来说他的Destination是某个队列或者主题
MessageProducer(消息生产者):消息生产者又session创建,并用于将消息发送导Desination。同样消息生产者分为两种类型:QueueSender和TopicPublisher,可以调用生产者的send或者publish方法发送消息
MessageConsumer:消费者由session创建,用于接受被发送到Destination的消息,同样有两种类型QueueReceiver和TopicSubscriber。
Message: 是在消费者和生产者之间传送的对象。一般有三部分组成,消息头,一个消息属性,一个消息体。