依赖
< dependencies>
< dependency>
< groupId> com. rabbitmq< / groupId>
< artifactId> amqp- client< / artifactId>
< version> 4.0 .3 < / version>
< / dependency>
< dependency>
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- logging< / artifactId>
< version> 2.1 .3 . RELEASE< / version>
< / dependency>
< dependency>
< groupId> org. apache. commons< / groupId>
< artifactId> commons- lang3< / artifactId>
< version> 3.3 .2 < / version>
< / dependency>
< / dependencies>
创建连接工具类
public class ConnectionUtil {
public static Connection getConnection ( ) throws Exception {
ConnectionFactory factory = new ConnectionFactory ( ) ;
factory. setHost ( "localhost" ) ;
factory. setPort ( 5672 ) ;
factory. setVirtualHost ( "/" ) ;
factory. setUsername ( "guest" ) ;
factory. setPassword ( "guest" ) ;
Connection connection = factory. newConnection ( ) ;
return connection;
}
}
生产者
import com. rabbitmq. client. Channel;
import com. rabbitmq. client. Connection;
public class Producer01 {
private final static String QUEUE_NAME = "test_queue" ;
public static void main ( String[ ] argv) throws Exception {
Connection connection = ConnectionUtil. getConnection ( ) ;
Channel channel = connection. createChannel ( ) ;
channel. queueDeclare ( QUEUE_NAME, false , false , false , null) ;
String message = "Hello World!" ;
channel. basicPublish ( "" , QUEUE_NAME, null, message. getBytes ( ) ) ;
System. out. println ( " [x] Sent '" + message + "'" ) ;
channel. close ( ) ;
connection. close ( ) ;
}
}
消费者1
import com. rabbitmq. client. Channel;
import com. rabbitmq. client. Connection;
import com. rabbitmq. client. QueueingConsumer;
import com. rabbitmq. client. QueueingConsumer. Delivery;
public class Consumer01 {
private final static String QUEUE_NAME = "test_queue" ;
public static void main ( String[ ] argv) throws Exception {
Connection connection = ConnectionUtil. getConnection ( ) ;
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 ) {
Delivery delivery = consumer. nextDelivery ( ) ;
System. out. println ( "老的消费者api收到消息:" + new String ( delivery. getBody ( ) ) ) ;
}
}
}
消费者2
import com. rabbitmq. client. *;
import java. io. IOException;
public class Consumer02 {
private final static String QUEUE_NAME = "test_queue" ;
public static void main ( String[ ] argv) throws Exception {
Connection connection = ConnectionUtil. getConnection ( ) ;
Channel channel = connection. createChannel ( ) ;
channel. queueDeclare ( QUEUE_NAME, false , false , false , null) ;
DefaultConsumer consumer = new DefaultConsumer ( channel) {
@Override
public void handleDelivery ( String consumerTag,
Envelope envelope,
AMQP. BasicProperties properties,
byte [ ] body)
throws IOException {
String exchange = envelope. getExchange ( ) ;
String routingKey = envelope. getRoutingKey ( ) ;
long deliveryTag = envelope. getDeliveryTag ( ) ;
String msg = new String ( body, "utf-8" ) ;
System. out. println ( "receive message.." + msg) ;
}
} ;
channel. basicConsume ( QUEUE_NAME, true , consumer) ;
}
}
运行结果
"C:\Program Files\Java\jdk1.8.0_45\bin\java.exe" "-javaagent:D:\IntelliJ IDEA 2018.2.5\lib\idea_rt.jar=2377:D:\IntelliJ IDEA 2018.2.5\bin" - Dfile. encoding= UTF- 8 - classpath "C: \Program Files\Java\jdk1. 8.0 _45\jre\lib\charsets. jar; C: \Program log4j12- 1.7 .7 . jar! / org/ slf4j/ impl/ StaticLoggerBinder. class ]
SLF4J: See http: / / www. slf4j. org/ codes. html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ ch. qos. logback. classic. util. ContextSelectorStaticBinder]
* * [ x] Sent 'Hello World!' * *
Process finished with exit code 0
"C:\Program Files\Java\jdk1.8.0_45\bin\java.exe" "- javaagent: D: \IntelliJ IDEA com. ly. rabitmq. Consumer02
receive message. . Hello World!
"C:\Program Files\Java\jdk1.8.0_45\bin\java.exe" "- javaagent: D: \IntelliJ IDEA com. ly. rabitmq. Consumer02
老的消费者api收到消息: Hello World!