Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Download RabbitMQ
- http://www.rabbitmq.com/download.html
- //couple commands run before start java app
- rabbitmq_server-3.6.0\sbin\rabbitmqctl status
- rabbitmq_server-3.6.0\sbin\rabbitmqctl stop
- rabbitmq_server-3.6.0\sbin\rabbitmqctl start
- rabbitmq_server-3.6.0\sbin\rabbitmq-server
- Send.java
- package co.uk.tedo.producent_jms.RabbitMQ;
- import com.rabbitmq.client.ConnectionFactory;
- import com.rabbitmq.client.Connection;
- import java.util.concurrent.TimeoutException;
- import com.rabbitmq.client.Channel;
- // TODO: Auto-generated Javadoc
- /**
- * The Class Send. RabbitMQ client send message
- */
- public class Send {
- /** The Constant QUEUE_NAME. */
- private final static String QUEUE_NAME = "hello";
- /** The connection. */
- static Connection connection;
- /**
- * The main method.
- *
- * @param argv the arguments
- * @throws IOException Signals that an I/O exception has occurred.
- * @throws TimeoutException
- */
- public static void main(String[] argv)
- throws java.io.IOException, TimeoutException {
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- //Connection connection;
- try {
- connection = factory.newConnection();
- } catch (TimeoutException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Channel channel = connection.createChannel();
- channel.queueDeclare(QUEUE_NAME, false, false, false, null);
- String message = "Witaj RabbitMQ";
- channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
- System.out.println(" [x] Sent '" + message + "'");
- channel.close();
- connection.close();
- }
- }
- Recv.java
- package co.uk.tedo.producent_jms.RabbitMQ;
- import com.rabbitmq.client.ConnectionFactory;
- import com.rabbitmq.client.Connection;
- import java.io.IOException;
- import java.util.concurrent.TimeoutException;
- import com.rabbitmq.client.AMQP;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Consumer;
- import com.rabbitmq.client.DefaultConsumer;
- import com.rabbitmq.client.Envelope;
- //TODO: Auto-generated Javadoc
- /**
- * The Class Send. RabbitMQ client received message
- */
- public class Recv {
- private final static String QUEUE_NAME = "hello";
- public static void main(String[] argv)
- throws java.io.IOException,
- java.lang.InterruptedException, TimeoutException {
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- Connection connection = factory.newConnection();
- Channel channel = connection.createChannel();
- channel.queueDeclare(QUEUE_NAME, false, false, false, null);
- System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
- Consumer consumer = new DefaultConsumer(channel) {
- @Override
- public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
- throws IOException {
- String message = new String(body, "UTF-8");
- System.out.println(" [x] Received '" + message + "'");
- }
- };
- channel.basicConsume(QUEUE_NAME, true, consumer);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement