1.创建一个类继承httpservlet 重写init() 方法在程序启动时进行消费
package com.bootdo.gi.util;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
/**
* Created by MACHENIKE on 2019/7/23.
*/
@Component
public class InitOsServlet extends HttpServlet {
@Override
@PostConstruct
public void init() throws ServletException {
try {
ActiveMqUtils.getMessage();
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.在这个类里边调用send方法放到队列里 调用get 或者getmessage 进行消费(两个方法的区别在于一个阻塞 一个非阻塞)
package com.bootdo.gi.util;
import com.bootdo.gi.dao.DeviceDao;
import com.rabbitmq.client.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;
public class ActiveMqUtils {
private final static String QUEUE_NAME = "healthQuee1"; //队列名
public static void send(String healthJson) throws IOException {
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("guest"); //账号
factory.setPassword("guest"); //密码
factory.setHost("localhost");
factory.setPort(5672);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = healthJson;
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
channel.close();
connection.close();
}
//阻塞
public static String get() throws Exception {
System.out.println("阻塞操作的mq");
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("guest");
factory.setPassword("guest");
factory.setHost("localhost");
factory.setVirtualHost("/");
factory.setPort(5672);
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");
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(QUEUE_NAME, true, consumer);
String message=null;
while(true) {
System.out.println(1);
//mq 通道中有数据 则 执行,没有则阻塞 consumer.nextDelivery();
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
System.out.println(2);
message = new String(delivery.getBody());
//httpclient
System.out.println(3);
System.out.println(" [x] Received '" + message + "'");
Thread.sleep(5000);
}
}
//非阻塞
public static void getMessage() throws IOException, Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
factory.setUsername("guest");
factory.setPassword("guest");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.err.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");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" [x] Received '" + message + "'");
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
}