rabbitmqUtil简单实现

private final static String QUEUE_NAME = "healthQuee";

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 + "'");
        return message;
    }


}

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 + "'");
            if(message!=null){
                // imei:111111,url:.......
                HealthDO healthDO= JSONObject.parseObject(message, HealthDO.class);
                String url=healthDO.getUrl();
                String imei=healthDO.getImei();
                healthDO.setBloodSugar("0");
                healthDO.setDbpInt(1);
                healthDO.setOxygen(2);
                healthDO.setSdpInt(3);
                healthDO.setHeartRate(666);
                String healthJson=JSONObject.toJSONString(healthDO);
                //调用httpclient------->url--->healthInfo=healthJson
                //
                HttpParamEntity httpParamEntity=new HttpParamEntity();
                httpParamEntity.getHeadParam().put("cpid","167");
                httpParamEntity.getHeadParam().put("key","6f5a3292c3c4731aec91df0a0581c278");
                httpParamEntity.getHeadParam().put("Content-Type","application/json");
                httpParamEntity.getHeadParam().put("imei", "356191942246206");
                System.out.println(url);
                httpParamEntity.setUrl(url);
                httpParamEntity.setBody(healthJson);

                HttpClientUtil httpClientUtil = new HttpClientUtil();
                String post = httpClientUtil.sendUrl(httpParamEntity, "post");
                System.out.println(post);
                System.out.println("发送成功--------------------------------------------------");
            }
        }
    };
   channel.basicConsume(QUEUE_NAME, true, consumer);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值