阿里推出了阿里云消息队列服务(https://www.aliyun.com/product/ons/),出于好奇,简单的尝试了一下它的功能及使用:
消息队列首先需要有它的主题Topic,生产者producer,消费者consumer。其中主题与生产者的对应关系为N:1,主题与消费者的对应关系为N:N。依次创建三者:
最后还需要accessKey,设置为:
(具体在哪里设置请详细参考阿里云消息队列的官方文档)
接下来就可以搭建自己的项目了。
pom.xml文件为:
<dependencies> <dependency> <groupId>com.aliyun.openservices</groupId> <artifactId>ons-client</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.6.RELEASE</version> </dependency> </dependencies>生产者与消费者的配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="producer" class="com.aliyun.openservices.ons.api.bean.ProducerBean" init-method="start" destroy-method="shutdown"> <property name="properties" > <!--生产者配置信息--> <props> <prop key="ProducerId">xxx</prop> <!--请替换XXX--> <prop key="AccessKey">xxx</prop> <prop key="SecretKey">xxx</prop> </props> </property> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="msgListener" class="com.wzj.messagequeue.MessageListener"></bean> <!--Listener配置--> <bean id="consumer" class="com.aliyun.openservices.ons.api.bean.ConsumerBean" init-method="start" destroy-method="shutdown"> <property name="properties" > <!--消费者配置信息--> <props> <prop key="ConsumerId">xxx</prop> <!--请替换XXX--> <prop key="AccessKey">xxx</prop> <prop key="SecretKey">xxx</prop> <!--将消费者线程数固定为50个.--> <prop key="ConsumeThreadNums">10</prop> </props> </property> <property name="subscriptionTable"> <map> <entry value-ref="msgListener"> <key> <bean class="com.aliyun.openservices.ons.api.bean.Subscription"> <property name="topic" value="Topic_Test_MQFunction"/> <property name="expression" value="*"/> </bean> </key> </entry> <!--更多的订阅添加entry节点即可--> </map> </property> </bean> </beans>之后ProductWithSpring则是消息的发出者实现:
public class ProduceWithSpring { public static void main(String[] args) { /** * 生产者Bean配置在producer.xml中,可通过ApplicationContext获取或者直接注入到其他类(比如具体的Controller)中. */ ApplicationContext context = new ClassPathXmlApplicationContext("producer.xml"); Producer producer = (Producer) context.getBean("producer"); //循环发送消息 for (int i = 0; i < 10; i++) { Message msg = new Message( // // Message Topic "Topic_Test_MQFunction", // Message Tag 可理解为Gmail中的标签,对消息进行再归类,方便Consumer指定过滤条件在MQ服务器过滤 "", // Message Body 可以是任何二进制形式的数据, MQ不做任何干预 // 需要Producer与Consumer协商好一致的序列化和反序列化方式 "Hello MQ".getBytes()); // 设置代表消息的业务关键属性,请尽可能全局唯一 // 以方便您在无法正常收到消息情况下,可通过MQ 控制台查询消息并补发 // 注意:不设置也不会影响消息正常收发 msg.setKey("Topic_Test_MQFunction_key"); // 发送消息,只要不抛异常就是成功 try { SendResult sendResult = producer.send(msg); assert sendResult != null; System.out.println("send success: " + sendResult.getMessageId()); }catch (ONSClientException e) { System.out.println("发送失败"); } } } }消费者接受的实现类ConsumeWithSpring:
public class ConsumeWithSpring { public static void main(String[] args) { /** * 消费者Bean配置在consumer.xml中,可通过ApplicationContext获取或者直接注入到其他类(比如具体的Controller)中. */ ApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml"); System.out.println("Consumer Started"); } }从消息队列接受到消息的事件监听:(注意它在consumer.xml中被定义的。)
public class MessageListener implements com.aliyun.openservices.ons.api.MessageListener { public Action consume(Message message, ConsumeContext context) { System.out.println("Receive: " + message.getMsgID()); try { //do something.. return Action.CommitMessage; }catch (Exception e) { //消费失败 return Action.ReconsumeLater; } } }
之后运行发送消息的ProduceWithSpring和接收消息的ConsumeWithSpring即可。但是我发现一个问题,有时候我向消息队列发送10条消息,有时候消费者只捕获到了小于10条的消息传回来,感觉是不是目前公测的消息队列还不是很稳定?