PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/tanxinzheng/activemq-api
Java | 125 lines | 111 code | 9 blank | 5 comment | 12 complexity | cc487343927ff8ffccea8f89a5943fd5 MD5 | raw file
  1. package com.udfex.ucs.amq.api;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.udfex.ucs.amq.api.service.ProducerService;
  4. import org.apache.activemq.ActiveMQConnectionFactory;
  5. import org.apache.activemq.ActiveMQSession;
  6. import org.apache.activemq.command.ActiveMQQueue;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.jms.core.JmsTemplate;
  11. import org.springframework.jms.core.MessageCreator;
  12. import org.springframework.jms.listener.DefaultMessageListenerContainer;
  13. import org.springframework.jms.listener.SessionAwareMessageListener;
  14. import javax.jms.*;
  15. import java.util.Properties;
  16. import java.util.UUID;
  17. /**
  18. * Created by Jeng on 16/1/31.
  19. */
  20. public class ProducerImpl implements Producer {
  21. private static final Logger logger = LoggerFactory.getLogger(ProducerImpl.class);
  22. private ProducerService producerService;
  23. private JmsTemplate jmsTemplate;
  24. private ActiveMQConnectionFactory connectionFactory;
  25. private ActiveMQQueue destination;
  26. private ActiveMQQueue responseQueueDestination;
  27. private DefaultMessageListenerContainer messageListenerContainer;
  28. public ProducerImpl(ActiveMQConnectionFactory activeMQConnectionFactory, ProducerService producerHandler, Properties properties) {
  29. connectionFactory = activeMQConnectionFactory;
  30. String queueName = (String) properties.get(ProducerConst.TOPIC);
  31. String responseQueueName = queueName + ".response";
  32. destination = new ActiveMQQueue(queueName);
  33. responseQueueDestination = new ActiveMQQueue(responseQueueName);
  34. producerService = producerHandler;
  35. jmsTemplate = new JmsTemplate();
  36. jmsTemplate.setConnectionFactory(connectionFactory);
  37. jmsTemplate.setDefaultDestination(destination);
  38. jmsTemplate.setSessionAcknowledgeMode(ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE);
  39. jmsTemplate.setSessionTransacted(false);
  40. messageListenerContainer = new DefaultMessageListenerContainer();
  41. messageListenerContainer.setSessionTransacted(false);
  42. messageListenerContainer.setSessionAcknowledgeMode(4);
  43. messageListenerContainer.setConnectionFactory(connectionFactory);
  44. messageListenerContainer.setDestination(responseQueueDestination);
  45. messageListenerContainer.setMessageListener(new SessionAwareMessageListener<TextMessage>() {
  46. @Override
  47. public void onMessage(TextMessage textMessage, Session session) throws JMSException {
  48. String messageId = textMessage.getStringProperty(BaseMessage.MESSAGE_ID);
  49. String requestId = textMessage.getStringProperty(BaseMessage.REQUEST_ID);
  50. String message = textMessage.getText();
  51. logger.debug("Producer MessageListener onMessage : " + message);
  52. try {
  53. ConsumeResult consumeResult = JSONObject.parseObject(message, ConsumeResult.class);
  54. if (ConsumeResult.CONSUME_SUCCESS_CODE == consumeResult.getResultCode()) {
  55. if(producerService != null){
  56. producerService.onHandlerSuccess(messageId, requestId, message);
  57. }
  58. }else{
  59. if(producerService != null){
  60. producerService.onHandlerFailure(messageId, requestId, message);
  61. }
  62. }
  63. textMessage.acknowledge();
  64. } catch (Exception e){
  65. e.printStackTrace();
  66. logger.error(e.getMessage(), e);
  67. }
  68. }
  69. });
  70. messageListenerContainer.initialize();
  71. messageListenerContainer.start();
  72. }
  73. public SendResult send(DataMessage dataMessage) {
  74. SendResult sendResult = new SendResult();
  75. // 发送前持久化消息数据,调用消息持久化接口
  76. if(dataMessage.getMessageId() == null){
  77. dataMessage.setMessageId(UUID.randomUUID().toString().replaceAll("\\-",""));
  78. }
  79. sendResult.setMessageId(dataMessage.getMessageId());
  80. final String messageId = dataMessage.getMessageId();
  81. final String requestId = dataMessage.getRequestId();
  82. final String msg = dataMessage.getMessageBody();
  83. try {
  84. jmsTemplate.send(destination, new MessageCreator() {
  85. public Message createMessage(Session session) throws JMSException {
  86. TextMessage message = session.createTextMessage(msg);
  87. message.setStringProperty(BaseMessage.MESSAGE_ID, messageId);
  88. message.setStringProperty(BaseMessage.REQUEST_ID, requestId);
  89. message.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
  90. message.setJMSReplyTo(responseQueueDestination);
  91. return message;
  92. }
  93. });
  94. if(producerService != null){
  95. producerService.onSendSuccess(sendResult.getMessageId());
  96. }
  97. sendResult.setResultCode(SendResult.SEND_SUCCESS);
  98. return sendResult;
  99. }catch (Exception e){
  100. e.printStackTrace();
  101. logger.error(e.getMessage(), e);
  102. sendResult.setResultCode(SendResult.SEND_FAILURE);
  103. sendResult.setResultMsg(e.getMessage());
  104. if(producerService != null){
  105. producerService.onSendFailure(sendResult.getMessageId(), e.getMessage());
  106. }
  107. return sendResult;
  108. }
  109. }
  110. public void start() {
  111. //messageListenerContainer.initialize();
  112. }
  113. public void shutdown() {
  114. messageListenerContainer.shutdown();
  115. }
  116. }