Advertisement
andrzejiwaniuk

JMS RabbitMQ

Jan 19th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. //Download RabbitMQ
  2. http://www.rabbitmq.com/download.html
  3.  
  4. //couple commands run before start java app
  5. rabbitmq_server-3.6.0\sbin\rabbitmqctl status
  6. rabbitmq_server-3.6.0\sbin\rabbitmqctl stop
  7. rabbitmq_server-3.6.0\sbin\rabbitmqctl start
  8. rabbitmq_server-3.6.0\sbin\rabbitmq-server
  9.  
  10. Send.java
  11.  
  12.  
  13.  
  14. package co.uk.tedo.producent_jms.RabbitMQ;
  15.  
  16. import com.rabbitmq.client.ConnectionFactory;
  17. import com.rabbitmq.client.Connection;
  18.  
  19. import java.util.concurrent.TimeoutException;
  20.  
  21. import com.rabbitmq.client.Channel;
  22.  
  23. // TODO: Auto-generated Javadoc
  24. /**
  25.  * The Class Send. RabbitMQ client send message
  26.  */
  27. public class Send {
  28.      
  29.     /** The Constant QUEUE_NAME. */
  30.     private final static String QUEUE_NAME = "hello";
  31.      
  32.     /** The connection. */
  33.     static Connection connection;
  34.      
  35.     /**
  36.      * The main method.
  37.      *
  38.      * @param argv the arguments
  39.      * @throws IOException Signals that an I/O exception has occurred.
  40.      * @throws TimeoutException
  41.      */
  42.     public static void main(String[] argv)
  43.           throws java.io.IOException, TimeoutException {
  44.          
  45.           ConnectionFactory factory = new ConnectionFactory();
  46.             factory.setHost("localhost");
  47.             //Connection connection;
  48.             try {
  49.                 connection = factory.newConnection();
  50.             } catch (TimeoutException e) {
  51.                 // TODO Auto-generated catch block
  52.                 e.printStackTrace();
  53.             }
  54.             Channel channel = connection.createChannel();
  55.             channel.queueDeclare(QUEUE_NAME, false, false, false, null);
  56.             String message = "Witaj RabbitMQ";
  57.             channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
  58.             System.out.println(" [x] Sent '" + message + "'");
  59.             channel.close();
  60.             connection.close();
  61.  
  62.       }
  63. }
  64.  
  65. Recv.java
  66. package co.uk.tedo.producent_jms.RabbitMQ;
  67.  
  68. import com.rabbitmq.client.ConnectionFactory;
  69. import com.rabbitmq.client.Connection;
  70.  
  71. import java.io.IOException;
  72. import java.util.concurrent.TimeoutException;
  73.  
  74. import com.rabbitmq.client.AMQP;
  75. import com.rabbitmq.client.Channel;
  76. import com.rabbitmq.client.Consumer;
  77. import com.rabbitmq.client.DefaultConsumer;
  78. import com.rabbitmq.client.Envelope;
  79. //TODO: Auto-generated Javadoc
  80. /**
  81. * The Class Send. RabbitMQ client received message
  82. */
  83. public class Recv {
  84.     private final static String QUEUE_NAME = "hello";
  85.  
  86.       public static void main(String[] argv)
  87.           throws java.io.IOException,
  88.                  java.lang.InterruptedException, TimeoutException {
  89.  
  90.         ConnectionFactory factory = new ConnectionFactory();
  91.         factory.setHost("localhost");
  92.         Connection connection = factory.newConnection();
  93.         Channel channel = connection.createChannel();
  94.  
  95.         channel.queueDeclare(QUEUE_NAME, false, false, false, null);
  96.         System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
  97.         Consumer consumer = new DefaultConsumer(channel) {
  98.             @Override
  99.             public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
  100.                 throws IOException {
  101.               String message = new String(body, "UTF-8");
  102.               System.out.println(" [x] Received '" + message + "'");
  103.             }
  104.           };
  105.           channel.basicConsume(QUEUE_NAME, true, consumer);
  106.         }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement