Guest User

Untitled

a guest
Jul 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. import org.apache.activemq.ActiveMQConnectionFactory;
  2.  
  3. import javax.jms.Connection;
  4. import javax.jms.DeliveryMode;
  5. import javax.jms.Destination;
  6. import javax.jms.MessageConsumer;
  7. import javax.jms.MessageProducer;
  8. import javax.jms.Session;
  9. import javax.jms.TextMessage;
  10.  
  11. public class Collector {
  12.  
  13. public String send(String payload, String meta) {
  14. try {
  15. // Create the connection
  16. ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
  17.  
  18. // Create a Connection
  19. Connection connection = connectionFactory.createConnection();
  20. connection.start();
  21.  
  22. // Create a Session
  23. Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  24.  
  25. Destination destination = session.createQueue("META.DATA");
  26.  
  27. MessageProducer producer = session.createProducer(destination);
  28. producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
  29.  
  30. // Create a messages
  31. TextMessage message = session.createTextMessage(meta);
  32.  
  33. // Tell the producer to send the message
  34. producer.send(message);
  35.  
  36. Session consumerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  37. Destination cDest = consumerSession.createQueue("RESPONSE");
  38.  
  39. MessageConsumer consumer = consumerSession.createConsumer(cDest);
  40. TextMessage msg = (TextMessage)consumer.receive();
  41.  
  42.  
  43. // Clean up
  44. session.close();
  45. consumerSession.close();
  46. connection.close();
  47.  
  48. return msg.getText();
  49.  
  50. }catch(Exception e) {
  51. e.printStackTrace();
  52. }
  53.  
  54. return null;
  55. }
  56.  
  57. }
  58.  
  59. import org.apache.activemq.ActiveMQConnectionFactory;
  60.  
  61. import javax.jms.Connection;
  62. import javax.jms.DeliveryMode;
  63. import javax.jms.Destination;
  64. import javax.jms.JMSException;
  65. import javax.jms.MessageConsumer;
  66. import javax.jms.MessageProducer;
  67. import javax.jms.Session;
  68. import javax.jms.TextMessage;
  69.  
  70. public class Server implements Runnable {
  71.  
  72. public static void main(String args[]) {
  73. Thread t = new Thread(new Server());
  74. t.start();
  75. }
  76.  
  77. public void run() {
  78. Connection connection = null;
  79. try {
  80. // Create the connection
  81. ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
  82.  
  83. // Create a Connection
  84. connection = connectionFactory.createConnection();
  85. connection.start();
  86. while (true) {
  87. Session cSession = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
  88. Destination cDest = cSession.createQueue("META.DATA");
  89. MessageConsumer consumer = cSession.createConsumer(cDest);
  90.  
  91. TextMessage msg = (TextMessage)consumer.receive();
  92.  
  93. String returnPayload = "You sent me " + msg.getText() + "? DAAAAAAMN!";
  94.  
  95. Session pSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  96. Destination pDest = pSession.createQueue("RESPONSE");
  97.  
  98. TextMessage msg2 = pSession.createTextMessage(returnPayload);
  99.  
  100. MessageProducer producer = pSession.createProducer(pDest);
  101.  
  102. producer.send(msg2);
  103. cSession.close();
  104. pSession.close();
  105. }
  106. }catch(Exception e) {
  107. e.printStackTrace();
  108. }finally {
  109. if (connection != null) {
  110. try {
  111. connection.close();
  112. } catch (JMSException e) {
  113. // TODO Auto-generated catch block
  114. e.printStackTrace();
  115. }
  116. }
  117. }
  118.  
  119. }
  120.  
  121. }
  122.  
  123.  
  124. public class Client {
  125.  
  126. public static void main(String args[]) {
  127. Collector c = new Collector();
  128.  
  129. System.out.println(c.send("Mah Dick", "DEEZ NUTS!"));
  130.  
  131. }
  132. }
Add Comment
Please, Sign In to add comment