Created
June 27, 2016 15:39
-
-
Save s4553711/3c97698636f8a999571d7ed8583834f2 to your computer and use it in GitHub Desktop.
RabbitMQ simple example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javac -cp rabbitmq-client.jar Send.java Receiv.java | |
java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar Receiv | |
echo "aaaa" | java -cp .:commons-io-1.2.jar:commons-cli-1.1.jar:rabbitmq-client.jar Send |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.rabbitmq.client.ConnectionFactory; | |
import com.rabbitmq.client.Connection; | |
import com.rabbitmq.client.Envelope; | |
import com.rabbitmq.client.AMQP; | |
import java.util.concurrent.TimeoutException; | |
import com.rabbitmq.client.Channel; | |
import com.rabbitmq.client.Consumer; | |
import com.rabbitmq.client.DefaultConsumer; | |
import java.io.IOException; | |
public class Receiv { | |
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("127.0.0.1"); | |
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.rabbitmq.client.ConnectionFactory; | |
import com.rabbitmq.client.Connection; | |
import com.rabbitmq.client.Channel; | |
import java.util.concurrent.TimeoutException; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
public class Send { | |
private final static String QUEUE_NAME = "hello"; | |
public static void main(String[] argv) throws java.io.IOException, TimeoutException { | |
ConnectionFactory factory = new ConnectionFactory(); | |
factory.setHost("localhost"); | |
Connection connection = factory.newConnection(); | |
Channel channel = connection.createChannel(); | |
channel.queueDeclare(QUEUE_NAME, false, false, false, null); | |
String message = "Hello World!"; | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String line; | |
while((line = br.readLine()) != null) { | |
channel.basicPublish("", QUEUE_NAME, null, line.getBytes()); | |
} | |
br.close(); | |
channel.close(); | |
connection.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment