PageRenderTime 35ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/udfex/ucs/amq/api/ConsumerImpl.java

https://gitlab.com/tanxinzheng/activemq-api
Java | 77 lines | 64 code | 10 blank | 3 comment | 2 complexity | daf50bf80fdfa436d701d432e0cbe54c MD5 | raw file
  1. package com.udfex.ucs.amq.api;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.apache.activemq.command.ActiveMQQueue;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.jms.listener.DefaultMessageListenerContainer;
  7. import org.springframework.jms.listener.SessionAwareMessageListener;
  8. import javax.jms.*;
  9. /**
  10. * Created by Jeng on 16/1/31.
  11. */
  12. public class ConsumerImpl implements Consumer {
  13. private static Logger logger = LoggerFactory.getLogger(ConsumerImpl.class);
  14. private DefaultMessageListenerContainer messageListenerContainer;
  15. public DefaultMessageListenerContainer getMessageListenerContainer() {
  16. return messageListenerContainer;
  17. }
  18. public void setMessageListenerContainer(DefaultMessageListenerContainer messageListenerContainer) {
  19. this.messageListenerContainer = messageListenerContainer;
  20. }
  21. @Override
  22. public void subscribe(String topic, final MessageToObject messageToObject, final MessageHandler messageHandler) {
  23. messageListenerContainer.setDestination(new ActiveMQQueue(topic));
  24. messageListenerContainer.setMessageListener(new SessionAwareMessageListener<TextMessage>() {
  25. @Override
  26. public void onMessage(final TextMessage message, Session session) throws JMSException {
  27. MessageProducer producer = session.createProducer(message.getJMSReplyTo());
  28. String requestId = message.getStringProperty(BaseMessage.REQUEST_ID);
  29. String messageId = message.getStringProperty(BaseMessage.MESSAGE_ID);
  30. ConsumeResult consumeResult = new ConsumeResult();
  31. try {
  32. consumeResult.setResultCode(ConsumeResult.CONSUME_SUCCESS_CODE);
  33. consumeResult.setResultMsg(ConsumeResult.CONSUME_SUCCESS_DEFAULT_MESSAGE);
  34. DataMessage dataMessage = new DataMessage();
  35. dataMessage.setMessageBody(message.getText());
  36. dataMessage.setMessageId(messageId);
  37. dataMessage.setRequestId(requestId);
  38. if(messageHandler != null){
  39. messageHandler.handler(messageToObject.fromMessage(dataMessage));
  40. }
  41. } catch (Exception e) {
  42. logger.error(e.getMessage(), e);
  43. e.printStackTrace();
  44. consumeResult.setResultCode(ConsumeResult.CONSUME_FAIL_CODE);
  45. consumeResult.setResultMsg(e.getMessage());
  46. } finally {
  47. consumeResult.setMessageId(messageId);
  48. consumeResult.setRequestId(requestId);
  49. TextMessage responseMessage = session.createTextMessage(JSONObject.toJSONString(consumeResult));
  50. responseMessage.setStringProperty(BaseMessage.MESSAGE_ID, messageId);
  51. responseMessage.setStringProperty(BaseMessage.REQUEST_ID, requestId);
  52. producer.send(responseMessage);
  53. message.acknowledge();
  54. }
  55. }
  56. });
  57. }
  58. @Override
  59. public void start() {
  60. messageListenerContainer.initialize();
  61. messageListenerContainer.start();
  62. }
  63. @Override
  64. public void shutdown() {
  65. messageListenerContainer.shutdown();
  66. }
  67. }