1. New project->maven->maven Project
2. Add to pom.xml:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>//从这里开始是加入的内容
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.6.5</version>
</dependency>//End of adding content
3. Consumers:
package RabbitMQMaven_P.test;
/**
* Hello world!
*
*/
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;
import com.rabbitmq.client.ShutdownSignalException;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
public class App
{
private final static String QUEUE_NAME = "hello";
public static void main( String[] args )
{
ConnectionFactory cf = new ConnectionFactory();
//rabbitmq listens to IP
cf.setHost("localhost");
//rabbitmq listens to the port by default, pay attention Remember to open the port
cf.setPort(5672);
//Set the user for access
cf.setUsername("admin");
cf.setPassword("admin");
try
{
//Create a connection
Connection conn = cf.newConnection();
//Create a message channel
Channel channel = conn.createChannel();
//Create a hello queue
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System. out.println("Waiting for msg....");
//Create a consumer and accept messages
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
throws IOException {
String msg = new String(body, "UTF-8");
System.out.println("Received is = '" + msg + "'");
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}catch(Exception e1)
{
System.out.println( "try 1" );
}
System.out.println( "Hello World!" );
}
}
4. Producer:
package RabbitMQMaven_P.test_P;
/**
* Hello world!
*
*/
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class App
{
private final static String QUEUE_NAME = "hello";
public static void main( String[] args )
{
ConnectionFactory cf = new ConnectionFactory();
//rabbitmq listens on IP
cf.setHost("localhost");
//rabbitmq listens to the port by default, remember to open it Port
cf.setPort(5672);
//Set the access user
cf.setUsername("admin");
cf.setPassword("admin");
try
{
//Create a connection
Connection conn = cf.newConnection();
//Create a message channel
Channel channel = conn.createChannel();
String msg = "hello world!!!! Hello~";
//Create a hello queue
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//Send message
channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
System.out.println("send msg "+ msg + " to ["+ QUEUE_NAME +"] queue !");
channel.close();
conn.close();
}catch(Exception e1)
{
System.out.println( "try 2" );
}
System.out.println( "Hello World2!" );
}
}
Remark:
If it is not try{}catch(){}, there will be problems such as timeout
refer to:
1、https://blog.csdn.net/qq1052441272/article/details/53743183
2、