返回顶部

收藏

使用JAVA实现RabbitMQ的发送告警

更多

基于rabbitmq实战4.2章节发送即忘模型,书上是使用PiPa库完成,这里使用java实现``` java package com.rayootech.rabbitmq.demo.inaction.alertwarning;

import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory;

public class AlertWarningProducer { private static final String EXCHANGE_NAME = "alerts"; private static final String ROUTING_KEY = "critical.alert"; public static void main(String[] argv) { Connection connection = null; Channel channel = null; try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("rabbitmq128"); factory.setUsername("admin"); factory.setPassword("admin"); factory.setPort(5672);

        connection = factory.newConnection();
        channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "topic",true);

        String message = "critical content!!!";

        channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, null, message.getBytes());
        System.out.println(" [x] Sent '" + ROUTING_KEY + "':'" + message + "'");

    }
    catch  (Exception e) {
        e.printStackTrace();
    }
    finally {
        if (connection != null) {
            try {
                connection.close();
            }
            catch (Exception ignore) {}
        }
    }
}

}

``` java None

标签:java

收藏

0人收藏

支持

0

反对

0

发表评论