一、activeMQ
默认已经安装完毕
启动,进入activemq目录下的bin目录,启动,./activemq start
关闭防火前或者打开端口
activemq监听的是61616,我们访要查看消息队列的状态时访问8161
java端操作消息队列使用的是61616端口
本地物理机访问8161
http://127.0.0.1:8161/admin/
弹出输入框,默认用户和密码为admin,查看状态
登录后,会有这么一些列
Number Of Pending Messages :等待消费的消息 这个是当前未出队列的数量。
Number Of Consumers :消费者 这个是消费者端的消费者数量,正在监听的消费端
Messages Enqueued :进入队列的消息 进入队列的总数量,包括出队列的。
Messages Dequeued :出了队列的消息 可以理解为是消费这消费掉的数量。
队列
点对点模式
点对点的模式主要建立在一个队列上面,当连接一个列队的时候,发送端不需要知道接 收端是否正在接收,
可以直接向 ActiveMQ 发送消息,发送的消息,将会先进入队列中,如 果有接收端在监听,
则会发向接收端,如果没有接收端接收,则会保存在 activemq 服务器, 直到接收端接收消息,
点对点的消息模式可以有多个发送端,多个接收端,但是一条消息, 只会被一个接收端给接收到,
哪个接收端先连上 ActiveMQ,则会先接收到,而后来的接收 端则接收不到那条消息。
服务提供方
public class Test {
private static final String ACTIVEMQ="tcp://192.168.182.130:61616";
private static final String QUEUE="que1";
public static void main(String[] args) throws Exception{
ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTIVEMQ);
Connection connection = activeMQConnectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(QUEUE);
MessageProducer producer = session.createProducer(queue);
TextMessage xxxx = session.createTextMessage("xxxx");
producer.send(xxxx);
producer.close();
session.close();
connection.close();
System.out.println("发生消息");
}
}
服务消消费方(一)
同步 阻塞
public class Cous {
private static final String ACTIVEMQ="tcp://192.168.182.130:61616";
private static final String QUEUE="que1";
public static void main(String[] args)throws Exception {
ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTIVEMQ);
Connection connection = activeMQConnectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(QUEUE);
MessageConsumer consumer = session.createConsumer(queue);
TextMessage textMessage=(TextMessage)consumer.receive();等待消息,有消息就获取消息,断开连接
System.out.println(textMessage.getText());
System.out.println("访问结束");
consumer.close();
session.close();
connection.close();
}
}
同步 阻塞
如果指定的队列没有消息,则会等待,知道队列有消息,获取消息,然后断开连接
服务消消费方(二)
public class Cous2 {
private static final String ACTIVEMQ="tcp://192.168.182.130:61616";
private static final String QUEUE="que1"