Search in sources :

Example 1 with DeclareOk

use of com.rabbitmq.client.AMQP.Exchange.DeclareOk in project jeesuite-libs by vakinge.

the class RabbitmqConsumerAdapter method start.

@Override
public void start() throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("");
    factory.setPort(101);
    factory.setUsername("");
    factory.setPassword("");
    Connection connect = factory.newConnection();
    Channel channel = connect.createChannel();
    // 声明exchange
    DeclareOk declareOk = channel.exchangeDeclare("my.fanout3", "fanout", true);
    channel.exchangeDeclare("test", "fanout");
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, "test", "");
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), "UTF-8");
        System.out.println(" [x] Received '" + message + "'");
    };
    channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
    });
}
Also used : DeclareOk(com.rabbitmq.client.AMQP.Exchange.DeclareOk) DeliverCallback(com.rabbitmq.client.DeliverCallback) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection) Channel(com.rabbitmq.client.Channel) MQConsumer(com.mendmix.amqp.MQConsumer) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) DeclareOk(com.rabbitmq.client.AMQP.Exchange.DeclareOk) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) DeliverCallback(com.rabbitmq.client.DeliverCallback)

Example 2 with DeclareOk

use of com.rabbitmq.client.AMQP.Exchange.DeclareOk in project Insights by CognizantOneDevOps.

the class EngineTestData method publishMessage.

public static void publishMessage(String queueName, String routingKey, String playload) throws InsightsCustomException {
    String exchangeName = ApplicationConfigProvider.getInstance().getAgentDetails().getAgentExchange();
    Connection connection = null;
    Channel channel = null;
    try {
        connection = RabbitMQConnectionProvider.getConnection();
        channel = connection.createChannel();
    } catch (IOException e) {
        log.error(e);
    }
    String message = new GsonBuilder().disableHtmlEscaping().create().toJson(playload);
    message = message.substring(1, message.length() - 1).replace("\\", "");
    try {
        DeclareOk exchangeResp = channel.exchangeDeclarePassive(exchangeName);
        channel.queueDeclare(queueName, true, false, false, RabbitMQConnectionProvider.getQueueArguments());
        channel.queueBind(queueName, exchangeName, routingKey);
        channel.basicPublish(exchangeName, routingKey, null, message.getBytes());
    // connection.close();
    } catch (IOException e) {
        try {
            channel.exchangeDeclare(exchangeName, MQMessageConstants.EXCHANGE_TYPE);
            channel.queueDeclare(queueName, true, false, false, RabbitMQConnectionProvider.getQueueArguments());
            channel.queueBind(queueName, exchangeName, routingKey);
            channel.basicPublish(exchangeName, routingKey, null, message.getBytes());
        // connection.close();
        } catch (IOException e1) {
            log.error(e1);
        }
    }
}
Also used : DeclareOk(com.rabbitmq.client.AMQP.Exchange.DeclareOk) GsonBuilder(com.google.gson.GsonBuilder) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) IOException(java.io.IOException)

Example 3 with DeclareOk

use of com.rabbitmq.client.AMQP.Exchange.DeclareOk in project jeesuite-libs by vakinge.

the class RabbitmqConsumerAdapter method start.

@Override
public void start() throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("");
    factory.setPort(101);
    factory.setUsername("");
    factory.setPassword("");
    Connection connect = factory.newConnection();
    Channel channel = connect.createChannel();
    // 声明exchange
    DeclareOk declareOk = channel.exchangeDeclare("my.fanout3", "fanout", true);
    channel.exchangeDeclare("test", "fanout");
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, "test", "");
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), "UTF-8");
        System.out.println(" [x] Received '" + message + "'");
    };
    channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
    });
}
Also used : DeclareOk(com.rabbitmq.client.AMQP.Exchange.DeclareOk) DeliverCallback(com.rabbitmq.client.DeliverCallback) MQConsumer(com.jeesuite.amqp.MQConsumer) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection) Channel(com.rabbitmq.client.Channel) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) DeclareOk(com.rabbitmq.client.AMQP.Exchange.DeclareOk) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) DeliverCallback(com.rabbitmq.client.DeliverCallback)

Example 4 with DeclareOk

use of com.rabbitmq.client.AMQP.Exchange.DeclareOk in project Insights by CognizantOneDevOps.

the class WebhookEngineTestData method publishMessage.

@SuppressWarnings("unused")
public void publishMessage(String queueName, String routingKey, String playload) throws IOException, TimeoutException, InsightsCustomException {
    String exchangeName = ApplicationConfigProvider.getInstance().getAgentDetails().getAgentExchange();
    Connection connection = null;
    Channel channel = null;
    connection = RabbitMQConnectionProvider.getConnection();
    channel = connection.createChannel();
    String message = new GsonBuilder().disableHtmlEscaping().create().toJson(playload);
    message = message.substring(1, message.length() - 1).replace("\\", "");
    DeclareOk exchangeResp;
    try {
        exchangeResp = channel.exchangeDeclarePassive(exchangeName);
        channel.queueDeclare(queueName, true, false, false, RabbitMQConnectionProvider.getQueueArguments());
        channel.queueBind(queueName, exchangeName, routingKey);
        channel.basicPublish(exchangeName, routingKey, null, message.getBytes());
    // connection.close();
    } catch (IOException e) {
        try {
            channel.exchangeDeclare(exchangeName, MQMessageConstants.EXCHANGE_TYPE);
            channel.queueDeclare(queueName, true, false, false, RabbitMQConnectionProvider.getQueueArguments());
            channel.queueBind(queueName, exchangeName, routingKey);
            channel.basicPublish(exchangeName, routingKey, null, message.getBytes());
        // connection.close();
        } catch (IOException e1) {
            LOG.error(e1);
        }
    }
}
Also used : DeclareOk(com.rabbitmq.client.AMQP.Exchange.DeclareOk) GsonBuilder(com.google.gson.GsonBuilder) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) IOException(java.io.IOException)

Aggregations

DeclareOk (com.rabbitmq.client.AMQP.Exchange.DeclareOk)4 Channel (com.rabbitmq.client.Channel)4 Connection (com.rabbitmq.client.Connection)4 GsonBuilder (com.google.gson.GsonBuilder)2 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)2 DeliverCallback (com.rabbitmq.client.DeliverCallback)2 IOException (java.io.IOException)2 MQConsumer (com.jeesuite.amqp.MQConsumer)1 MQConsumer (com.mendmix.amqp.MQConsumer)1