Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.activemq.ActiveMQConnectionFactory;
- import javax.jms.Connection;
- import javax.jms.DeliveryMode;
- import javax.jms.Destination;
- import javax.jms.MessageConsumer;
- import javax.jms.MessageProducer;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- public class Collector {
- public String send(String payload, String meta) {
- try {
- // Create the connection
- ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
- // Create a Connection
- Connection connection = connectionFactory.createConnection();
- connection.start();
- // Create a Session
- Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- Destination destination = session.createQueue("META.DATA");
- MessageProducer producer = session.createProducer(destination);
- producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
- // Create a messages
- TextMessage message = session.createTextMessage(meta);
- // Tell the producer to send the message
- producer.send(message);
- Session consumerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- Destination cDest = consumerSession.createQueue("RESPONSE");
- MessageConsumer consumer = consumerSession.createConsumer(cDest);
- TextMessage msg = (TextMessage)consumer.receive();
- // Clean up
- session.close();
- consumerSession.close();
- connection.close();
- return msg.getText();
- }catch(Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- }
- import org.apache.activemq.ActiveMQConnectionFactory;
- import javax.jms.Connection;
- import javax.jms.DeliveryMode;
- import javax.jms.Destination;
- import javax.jms.JMSException;
- import javax.jms.MessageConsumer;
- import javax.jms.MessageProducer;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- public class Server implements Runnable {
- public static void main(String args[]) {
- Thread t = new Thread(new Server());
- t.start();
- }
- public void run() {
- Connection connection = null;
- try {
- // Create the connection
- ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
- // Create a Connection
- connection = connectionFactory.createConnection();
- connection.start();
- while (true) {
- Session cSession = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
- Destination cDest = cSession.createQueue("META.DATA");
- MessageConsumer consumer = cSession.createConsumer(cDest);
- TextMessage msg = (TextMessage)consumer.receive();
- String returnPayload = "You sent me " + msg.getText() + "? DAAAAAAMN!";
- Session pSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- Destination pDest = pSession.createQueue("RESPONSE");
- TextMessage msg2 = pSession.createTextMessage(returnPayload);
- MessageProducer producer = pSession.createProducer(pDest);
- producer.send(msg2);
- cSession.close();
- pSession.close();
- }
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- if (connection != null) {
- try {
- connection.close();
- } catch (JMSException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- }
- public class Client {
- public static void main(String args[]) {
- Collector c = new Collector();
- System.out.println(c.send("Mah Dick", "DEEZ NUTS!"));
- }
- }
Add Comment
Please, Sign In to add comment