RabbitMQ, rabbitmqandroid
RabbitMQ is a robust, easy-to-use, open-source message broker that supports multiple operating systems and languages.
Of course, the premise is that the machine is running rabbitmq-server.
Click the image below to download it:
What is the relationship between rabbitMQ and AMQP? What is rabbitMQ responsible?
That is the one between provider and consumer.
Message broker, such as ActiveMQ and RabbitMQ, can simply send and receive messages.
I followed the official Tutorial to write a simple Hello World.
Implement a small program from producer to queue to consumer.
Do not forget to add the java client:
<dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>3.3.4</version></dependency>
Provider:
Private final static String QUEUE_NAME = "hello"; public static void main (String [] args) throws IOException {// create Connection ConnectionFactory factory = new ConnectionFactory (); factory. setHost ("localhost"); Connection connection Connection = factory. newConnection (); Channel channel = connection. createChannel (); // defines the target queue channel. queueDeclare (QUEUE_NAME, false, null); String message = "Hello Worl D! "; System. out. println ("[x] Sent '" + message + "'"); channel. basicPublish ("", QUEUE_NAME, null, message. getBytes (); channel. close (); connection. close ();}
The process of the consumer er is roughly the same as that of the provider. You only need to note that the QUEUE_NAME must be the same:
private final static String QUEUE_NAME = "hello"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(QUEUE_NAME, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'" + new Date()); }}
If nothing happens, consumer can print "Hello World! ".
(PS: If the disk is too full, messages may not be received. You can set the disk_free_limit item in the configuration file .)