PageRenderTime 81ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/com/udfex/demo/activemq/ConsumerClient.java

https://gitlab.com/tanxinzheng/activemq-api
Java | 54 lines | 43 code | 6 blank | 5 comment | 1 complexity | 0e0965c8954fff03b918582da24fd64e MD5 | raw file
  1. package com.udfex.demo.activemq;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.udfex.ucs.amq.api.*;
  4. import org.apache.activemq.ActiveMQConnectionFactory;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import java.util.Properties;
  7. /**
  8. * Created by Jeng on 16/1/31.
  9. */
  10. public class ConsumerClient {
  11. public static void main(String[] args) {
  12. Properties properties = new Properties();
  13. //properties.put(PropertyKeyConst.AccessKey, "4ZY3Hr1k3VSOi6PW");
  14. //properties.put(PropertyKeyConst.SecretKey, "OM44UesEeGvmxfpu3LKtb4PvzVTw55");
  15. ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
  16. activeMQConnectionFactory.setBrokerURL("failover:(tcp://192.168.8.211:61616)");
  17. IAMQFactory iamqFactory = new IAMQFactory(activeMQConnectionFactory);
  18. Consumer consumer = iamqFactory.createConsumer(properties);
  19. consumer.subscribe("UUUUUU1", new MessageToObject<MockData>() {
  20. @Override
  21. public MockData fromMessage(DataMessage dataMessage) {
  22. return JSONObject.parseObject(dataMessage.getMessageBody(), MockData.class);
  23. }
  24. }, new MessageHandler<MockData>() {
  25. @Override
  26. public void handler(MockData message) {
  27. System.out.println("消费,GUEST.PID_UDFEX_PRODUCER_TEST_00001:" + JSONObject.toJSONString(message));
  28. if(message.getAge() %2 ==0){
  29. throw new IllegalArgumentException("不是偶数");
  30. }
  31. }
  32. });
  33. consumer.start();
  34. Consumer consumer2 = iamqFactory.createConsumer(properties);
  35. consumer2.subscribe("GUEST.PID_UDFEX_PRODUCER_TEST_00002", new MessageToObject<MockData2>() {
  36. @Override
  37. public MockData2 fromMessage(DataMessage dataMessage) {
  38. return JSONObject.parseObject(dataMessage.getMessageBody(), MockData2.class);
  39. }
  40. }, new MessageHandler<MockData2>() {
  41. @Override
  42. public void handler(MockData2 message) {
  43. System.out.println("消费,GUEST.PID_UDFEX_PRODUCER_TEST_00002:" + message.getName());
  44. }
  45. });
  46. consumer2.start();
  47. }
  48. }