package com.gi.util;
import java.io.IOException;
import com.alibaba.fastjson.JSONObject;
import com.gi.entrty.HealthDO;
import com.gi.entrty.HttpParamEntity;
import com.rabbitmq.client.*;
public class ActiveMqUtils {
private final static String QUEUE_NAME = "healthQueue";
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.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");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" [x] Received '" + message + "'");
// imei:111111,url:.......
HealthDO healthDO= JSONObject.parseObject(message, HealthDO.class);
String url=healthDO.getUrl();
String imei=healthDO.getImei();
System.out.println("检测心率数据");
healthDO.setBloodsugar("0");
healthDO.setDbpint(1);
healthDO.setOxygen(2);
healthDO.setSdpint(3);
String healthJson=JSONObject.toJSONString(healthDO);
//调用httpclient------->url--->healthInfo == healthJson
HttpParamEntity httpParamEntity = new HttpParamEntity();
httpParamEntity.getHeadParam().put("Content-Type","application/json");
httpParamEntity.setUrl(url);
httpParamEntity.getHeadParam().put("imei",imei);
httpParamEntity.setBody(healthJson);
System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"+httpParamEntity);
HttpClientUtil.sendUrl(httpParamEntity, "post");
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
public static void main(String[] args) throws Exception {
ActiveMqUtils.send("哈哈哈");
ActiveMqUtils.getMessage();
System.out.println("响应用户");
}
}
rabbitmq工具类
最新推荐文章于 2022-05-13 18:15:37 发布