----------------------------------------------------------------------------------------
Destination高级特性----->Composite Destinations
组合队列Composite Destinations : 允许用一个虚拟的destination代表多个destinations,这样就可以通过composite destinations在一个操作中同时向多个queue/topic发送消息。
有两种实现方式:
第一种:在客户端编码实现
第二种:在activemq.xml配置文件中实现
第一种:在客户端编码实现
在composite destinations中,多个destination之间采用","分隔。如下:这里有2个destination "my-queue1"和"my-queue2"
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("my-queue1,my-queue2");
消息发送程序:JmsSend.java
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class JmsSend {
public static void main(String[] args) throws Exception {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.1.81:61616");
Connection connection = connectionFactory.createConnection();
connection.start(); Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("my-queue1,my-queue2");
MessageProducer producer = session.createProducer(destination);
for(int i = 0;i < 5;i++){
TextMessage message = session.createTextMessage("messageAAA --->" + i);
//通过生产者发出消息
producer.send(message);
}
session.commit();
session.close();
connection.close();
}
}
运行消息发送程序后,去访问http://192.168.1.81:8161/admin/queues.jsp,可以看到如下:
第一个消费者程序:JmsReceiver.java, 从"my-queue1"这个destination中消费消息
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class JmsReceiver {
public static void main(String[] args) throws Exception {
ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://192.168.1.81:61616");
Connection connection = cf.createConnection();
connection.start(); Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("my-queue1");
MessageConsumer consumer = session.createConsumer(destination);
int i = 0;
while(i < 5){
Thread.sleep(1000);
i++;
TextMessage message = (TextMessage)consumer.receive();
session.commit();
System.out.println("111接收到的消息是:"+message.getText());
}
session.close();
connection.close();
}
}
运行结果:
再去访问http://192.168.1.81:8161/admin/queues.jsp,可以看到如下:
第二个消费者程序:JmsReceiver2.java, 从"my-queue2"这个destination中消费消息
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class JmsReceiver2 {
public static void main(String[] args) throws Exception {
ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://192.168.1.81:61616");
Connection connection = cf.createConnection();
connection.start(); Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("my-queue2");
MessageConsumer consumer = session.createConsumer(destination);
int i = 0;
while(i < 5){
Thread.sleep(1000);
i++;
TextMessage message = (TextMessage)consumer.receive();
session.commit();
System.out.println("222接收到的消息是:"+message.getText());
}
session.close();
connection.close();
}
}
运行结果:
再去访问http://192.168.1.81:8161/admin/queues.jsp,可以看到如下:
第二种:在activemq.xml配置文件中实现
消息发送程序,JmsSend.java
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class JmsSend {
public static void main(String[] args) throws Exception {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.1.81:61616");
Connection connection = connectionFactory.createConnection();
connection.start(); Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("MY.QUEUE");
MessageProducer producer = session.createProducer(destination);
for(int i = 0;i < 5;i++){
TextMessage message = session.createTextMessage("messageAAA --->" + i);
//通过生产者发出消息
producer.send(message);
}
session.commit();
session.close();
connection.close();
}
}
运行后,去访问http://192.168.1.81:8161/admin/queues.jsp,可以看到如下:
消费者程序1和2 与上面的代码一样,这里就不重复了。
Destination高级特性----->Configure Startup Destinations
在启动ActiveMQ的时候如果需要创建Destination的话,可以在activemq.xml中配置
启动activemq后去访问,http://192.168.1.81:8161/admin
Destination高级特性----->Delete Inactive Destinations
在ActiveMQ的queue在不使用之后,可以通过web控制台或者JMX方式来删除掉,当然,也可以通过配置,使得broker可以自动探测到无用的队列并删除掉,回收响应资源。
说明:
schedulePeriodForDestinationPurge: 设置多长时间检查一次,这里是10秒。
inactiveTimoutBeforeGC: 设置当Destination为空后,多长时间被删除,这里是30秒。
gcInactiveDestinations:设置删除掉不活动的队列,默认为false
Destination高级特性----->wildcars(通配符)
Wildcars用来支持名字分层体系,它不是JMS规范的一部分,是ActiveMQ的扩展。
ActiveMQ支持以下三种wildcars:
. 用于作为路径上名字间的分隔符
* 用于匹配路径上的任何名字
> 用于递归地匹配任何以这个名字开始的destination