ActiveMQ.wildcards

本文介绍ActiveMQ中通配符的使用方法,包括分隔符、任意字符匹配及后续所有字符匹配等,并提供Java代码示例,演示如何实现消息的群发与同步接收。
ActiveMQ的通配符官方介绍地址:http://activemq.apache.org/wildcards.html

1、 . 用了分隔地址。

2、* 某段地址的所有字符。

3、> 某段地址之后的所有字符。

1、的例子一个QueueName:Q.T.Test。

Q.T.Test就是当OnMessage监听这个地址的时候,只能收到这个发送到这个地址的得消息。

2、的例子一个QueueName:Q.*.Test,Q.*.*。

Q.*.Test就是在监听的时候收到(Q.T.Test,Q.T1.Test,Q.T2.Test中间的所有字符的地址)的消息。

3、的例子一个QueueName:Q.>,Q.T.>。

Q.>就是当监听的时候收到(Q.T,Q.T.Test,Q.T.Test,T之后的字符的)的消息。

下面是测试代码,发送使用composite destinations( 类似群发方式)官方参考地址:http://activemq.apache.org/composite-destinations.html

接收使用同步接收消息,如果用异步接收需要实现监听:

package testsend;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;


public class MessageService {


public static void main(String[] args) throws Exception{
String url = "tcp://127.0.0.1:61616";
String userName = "test";
String password = "test";
MessageService messageService = new MessageService();

System.out.println("send message....");
messageService.sendMessage(url,userName, password);
System.out.println("receive message....");
messageService.receiveMessage(url, userName, password);


}

public void sendMessage(String url,String userName,String password)throws Exception{
ActiveMQConnectionFactory connf = new ActiveMQConnectionFactory(url);
Connection conn = connf.createConnection(userName, password);
conn.start();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination dest = new ActiveMQQueue("Test.A,Test.B,Test.C");
Message msg = session.createTextMessage("test");
MessageProducer mp = session.createProducer(dest);
mp.send(msg);
System.out.println("send ok....");
mp.close();
session.close();
conn.close();
}

public void receiveMessage(String url,String userName,String password)throws Exception{
ActiveMQConnectionFactory connf = new ActiveMQConnectionFactory(url);
Connection conn = connf.createConnection(userName, password);
conn.setClientID("msgTest");
conn.start();
Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination dest = session.createQueue("Test.*");
MessageConsumer mc = session.createConsumer(dest);

int mu = 3;
for(int i=0;i<mu;i++){
Message message = mc.receive(1000*10);
System.out.println("接受次数:"+(i+1));
if(message!=null){
TextMessage tm = (TextMessage)message;
System.out.println("QueueName:"+tm.getJMSDestination());
System.out.println("mesg"+tm.getText());
}else{
System.out.println("message is null.....");
}
}

mc.close();
session.close();
conn.close();

}

测试结果:

send message....
send ok....
receive message....
接受次数:1
QueueName:queue://Test.C
mesg:test
接受次数:2
QueueName:queue://Test.A
mesg:test
接受次数:3
QueueName:queue://Test.B
mesg:test
2025-12-01 15:27:47,862 | INFO | Refreshing org.apache.activemq.xbean.XBeanBrokerFactory$1@6e1567f1: startup date [Mon Dec 01 15:27:47 CST 2025]; root of context hierarchy | org.apache.activemq.xbean.XBeanBrokerFactory$1 | main 2025-12-01 15:27:48,126 | ERROR | Failed to load: class path resource [activemq.xml], reason: Line 43 in XML document from class path resource [activemq.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 43; columnNumber: 26; cvc-complex-type.2.4.a: 发现了以元素 'whitelistClassFilter' 开头的无效内容。应以 '{"http://activemq.apache.org/schema/core":authorizationPlugin, "http://activemq.apache.org/schema/core":camelRoutesBrokerPlugin, "http://activemq.apache.org/schema/core":connectionDotFilePlugin, "http://activemq.apache.org/schema/core":destinationDotFilePlugin, "http://activemq.apache.org/schema/core":destinationPathSeparatorPlugin, "http://activemq.apache.org/schema/core":destinationsPlugin, "http://activemq.apache.org/schema/core":discardingDLQBrokerPlugin, "http://activemq.apache.org/schema/core":forcePersistencyModeBrokerPlugin, "http://activemq.apache.org/schema/core":jaasAuthenticationPlugin, "http://activemq.apache.org/schema/core":jaasCertificateAuthenticationPlugin, "http://activemq.apache.org/schema/core":jaasDualAuthenticationPlugin, "http://activemq.apache.org/schema/core":loggingBrokerPlugin, "http://activemq.apache.org/schema/core":multicastTraceBrokerPlugin, "http://activemq.apache.org/schema/core":partitionBrokerPlugin, "http://activemq.apache.org/schema/core":redeliveryPlugin, "http://activemq.apache.org/schema/core":runtimeConfigurationPlugin, "http://activemq.apache.org/schema/core":simpleAuthenticationPlugin, "http://activemq.apache.org/schema/core":statisticsBrokerPlugin, "http://activemq.apache.org/schema/core":timeStampingBrokerPlugin, "http://activemq.apache.org/schema/core":traceBrokerPathPlugin, "http://activemq.apache.org/schema/core":udpTraceBrokerPlugin, "http://activemq.apache.org/schema/core":virtualSelectorCacheBrokerPlugin, WC[##other:"http://activemq.apache.org/schema/core"]}' 之一开头。 | org.apache.activemq.xbean.XBeanBrokerFactory | main org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 43 in XML document from class path resource [activemq.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 43; columnNumber: 26; cvc-complex-type.2.4.a: 发现了以元素 'whitelistClassFilter' 开头的无效内容。应以 '{"http://activemq.apache.org/schema/core":authorizationPlugin, "http://activemq.apache.org/schema/core":camelRoutesBrokerPlugin, "http://activemq.apache.org/schema/core":connectionDotFilePlugin, "http://activemq.apache.org/schema/core":destinationDotFilePlugin, "http://activemq.apache.org/schema/core":destinationPathSeparatorPlugin, "http://activemq.apache.org/schema/core":destinationsPlugin, "http://activemq.apache.org/schema/core":discardingDLQBrokerPlugin, "http://activemq.apache.org/schema/core":forcePersistencyModeBrokerPlugin, "http://activemq.apache.org/schema/core":jaasAuthenticationPlugin, "http://activemq.apache.org/schema/core":jaasCertificateAuthenticationPlugin, "http://activemq.apache.org/schema/core":jaasDualAuthenticationPlugin, "http://activemq.apache.org/schema/core":loggingBrokerPlugin, "http://activemq.apache.org/schema/core":multicastTraceBrokerPlugin, "http://activemq.apache.org/schema/core":partitionBrokerPlugin, "http://activemq.apache.org/schema/core":redeliveryPlugin, "http://activemq.apache.org/schema/core":runtimeConfigurationPlugin, "http://activemq.apache.org/schema/core":simpleAuthenticationPlugin, "http://activemq.apache.org/schema/core":statisticsBrokerPlugin, "http://activemq.apache.org/schema/core":timeStampingBrokerPlugin, "http://activemq.apache.org/schema/core":traceBrokerPathPlugin, "http://activemq.apache.org/schema/core":udpTraceBrokerPlugin, "http://activemq.apache.org/schema/core":virtualSelectorCacheBrokerPlugin, WC[##other:"http://activemq.apache.org/schema/core"]}' 之一开头。 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:399)[spring-beans-4.3.18.RELEASE.jar:4.3.18.RELEASE] at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)[spring-beans-4.3.18.RELEASE.jar:4.3.18.RELEASE] at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)[spring-beans-4.3.18.RELEASE.jar:4.3.18.RELEASE] at org.apache.xbean.spring.context.ResourceXmlApplicationContext.loadBeanDefinitions(ResourceXmlApplicationContext.java:111)[xbean-spring-4.2.jar:4.2] at org.apache.xbean.spring.context.ResourceXmlApplicationContext.loadBeanDefinitions(ResourceXmlApplicationContext.java:104)[xbean-spring-4.2.jar:4.2] at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE] at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:614)[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:515)[spring-context-4.3.18.RELEASE.jar:4.3.18.RELEASE] at org.apache.xbean.spring.context.ResourceXmlApplicationContext.<init>(ResourceXmlApplicationContext.java:64)[xbean-spring-4.2.jar:4.2] at org.apache.xbean.spring.context.ResourceXmlApplicationContext.<init>(ResourceXmlApplicationContext.java:52)[xbean-spring-4.2.jar:4.2] at org.apache.activemq.xbean.XBeanBrokerFactory$1.<init>(XBeanBrokerFactory.java:104)[activemq-spring-5.15.9.jar:5.15.9] at org.apache.activemq.xbean.XBeanBrokerFactory.createApplicationContext(XBeanBrokerFactory.java:104)[activemq-spring-5.15.9.jar:5.15.9] at org.apache.activemq.xbean.XBeanBrokerFactory.createBroker(XBeanBrokerFactory.java:67)[activemq-spring-5.15.9.jar:5.15.9] at org.apache.activemq.broker.BrokerFactory.createBroker(BrokerFactory.java:71)[activemq-broker-5.15.9.jar:5.15.9] at org.apache.activemq.broker.BrokerFactory.createBroker(BrokerFactory.java:54)[activemq-broker-5.15.9.jar:5.15.9] at org.apache.activemq.console.command.StartCommand.runTask(StartCommand.java:87)[activemq-console-5.15.9.jar:5.15.9] at org.apache.activemq.console.command.AbstractCommand.execute(AbstractCommand.java:63)[activemq-console-5.15.9.jar:5.15.9] at org.apache.activemq.console.command.ShellCommand.runTask(ShellCommand.java:154)[activemq-console-5.15.9.jar:5.15.9] at org.apache.activemq.console.command.AbstractCommand.execute(AbstractCommand.java:63)[activemq-console-5.15.9.jar:5.15.9] at org.apache.activemq.console.command.ShellCommand.main(ShellCommand.java:104)[activemq-console-5.15.9.jar:5.15.9] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.8.0_181] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)[:1.8.0_181] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_181] at java.lang.reflect.Method.invoke(Method.java:498)[:1.8.0_181] at org.apache.activemq.console.Main.runTaskClass(Main.java:262)[activemq.jar:5.15.9] at org.apache.activemq.console.Main.main(Main.java:115)[activemq.jar:5.15.9]
最新发布
12-02
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值