Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. Properties:
  2. activemq.broker-url=(tcp://172.18.13.68:61616,tcp://172.18.13.69:61617)?ha=true&retryInterval=1000&reconnectAttempts=-1
  3. activemq.user=admin
  4. activemq.password=admin
  5. server.port=8082
  6.  
  7. Code:
  8. Configuration class:
  9.  
  10. @Value("${activemq.broker-url}")
  11. private String activeMqBrokerUri;
  12.  
  13. @Value("${activemq.user}")
  14. private String username;
  15.  
  16. @Value("${activemq.password}")
  17. private String password;
  18.  
  19. @Bean
  20. public Connection connection() throws JMSException {
  21. ActiveMQConnectionFactory connFactory = new ActiveMQConnectionFactory(activeMqBrokerUri, username, password);
  22. Connection connection = connFactory.createConnection();
  23. connection.start();
  24. return connection;
  25. }
  26.  
  27.  
  28. Sender Class:
  29.  
  30. public void sendDummyMessagesToTopic(String topicName) {
  31. Session session = null;
  32. MessageProducer msgProducer = null;
  33. try {
  34. session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  35. msgProducer = session.createProducer(session.createTopic(topicName));
  36. System.out.println("Iniciando envío...." + new Date());
  37. for (int i = 0; i < NUM_MESSAGES; i++) {
  38. String msg = "Message "+ i;
  39. TextMessage textMessage = session.createTextMessage(msg);
  40. try {
  41. msgProducer.send(textMessage);
  42. } catch (Exception e) {
  43. System.out.println("Fail sending message:" + i + ". Retrying...");
  44. i--;
  45. }
  46. }
  47. } catch (JMSException e) {
  48. System.out.println("Caught exception: " + e.getMessage());
  49. }
  50. try {
  51. if (msgProducer != null) {
  52. msgProducer.close();
  53. }
  54. if (session != null) {
  55. session.close();
  56. }
  57.  
  58. } catch (Throwable ignore) {
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement