Example usage for com.rabbitmq.client Channel queueDeclare

List of usage examples for com.rabbitmq.client Channel queueDeclare

Introduction

In this page you can find the example usage for com.rabbitmq.client Channel queueDeclare.

Prototype

Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
        Map<String, Object> arguments) throws IOException;

Source Link

Document

Declare a queue

Usage

From source file:rabbitmq_clienttest.Simple_receiver.java

public static void main(String[] argv) throws Exception {

    db = new Database_connector_sqlite();

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setUsername("es");
    factory.setPassword("a");
    //factory.setVirtualHost("/");
    factory.setPort(5672);//from  w w w .j a va 2  s  .c  om
    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");
            System.out.println(" [x] Received '" + message + "'");
            doWork(message);

        }
    };
    channel.basicConsume(QUEUE_NAME, true, consumer);

}

From source file:reactor.rabbitmq.ConnectionRecoveryTests.java

License:Open Source License

@BeforeEach
public void init() throws Exception {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.useNio();//from ww  w  .  j av  a 2 s  . co  m
    connectionFactory.setNetworkRecoveryInterval(RECOVERY_INTERVAL);
    connection = connectionFactory.newConnection();
    Channel channel = connection.createChannel();
    String queueName = UUID.randomUUID().toString();
    queue = channel.queueDeclare(queueName, false, false, false, null).getQueue();
    channel.close();
    receiver = null;
    sender = null;
    connectionMono = Mono.just(connectionFactory.newConnection()).cache();
}

From source file:reactor.rabbitmq.RabbitFluxTests.java

License:Open Source License

@BeforeEach
public void init() throws Exception {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.useNio();//from   w  w  w  .  jav  a 2s.  co m
    connection = connectionFactory.newConnection();
    Channel channel = connection.createChannel();
    String queueName = UUID.randomUUID().toString();
    queue = channel.queueDeclare(queueName, false, false, false, null).getQueue();
    channel.close();
    receiver = null;
    sender = null;
}

From source file:reactor.rabbitmq.ReactorRabbitMqTests.java

License:Open Source License

@Before
public void init() throws Exception {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.useNio();/*from w  ww . j  av a 2  s.  co m*/
    connection = connectionFactory.newConnection();
    Channel channel = connection.createChannel();
    String queueName = UUID.randomUUID().toString();
    queue = channel.queueDeclare(queueName, false, false, false, null).getQueue();
    channel.close();
    receiver = null;
    sender = null;
}

From source file:reactor.rabbitmq.RequestReplyTests.java

License:Open Source License

@BeforeAll
public static void initAll() throws Exception {
    try (Connection c = new ConnectionFactory().newConnection()) {
        Channel ch = c.createChannel();
        for (String queue : QUEUES) {
            ch.queueDeclare(queue, false, false, false, null);
        }/*from w  w w .ja v  a 2s . c o  m*/
    }
}

From source file:reactor.rabbitmq.SenderBenchmarkUtils.java

License:Open Source License

public static String declareQueue(Connection connection) throws Exception {
    String queueName = UUID.randomUUID().toString();
    Channel channel = connection.createChannel();
    String queue = channel.queueDeclare(queueName, false, false, false, null).getQueue();
    channel.close();/*  www . j a v  a 2 s.co  m*/
    return queue;
}

From source file:reactor.rabbitmq.SenderTests.java

License:Open Source License

@BeforeEach
public void init() throws Exception {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.useNio();//w  ww. j a  v a 2 s . c om
    connection = connectionFactory.newConnection();
    Channel channel = connection.createChannel();
    String queueName = UUID.randomUUID().toString();
    queue = channel.queueDeclare(queueName, false, false, false, null).getQueue();
    channel.close();
    sender = null;
}

From source file:ru.kinomir.queue.QueueSender.java

public synchronized void sendToQueue(Object data, String queueName, String queueHost, String userName,
        String password, String port, String virtualHost) {
    Channel channel = null;
    Connection connection = null;
    try {//  ww  w.j a v  a  2 s . c o  m
        logger.info("Send message to queue '" + queueName + "'");
        ConnectionFactory factory = new ConnectionFactory();
        if (!StringTools.isEmpty(userName)) {
            factory.setUsername(userName);
        }
        if (!StringTools.isEmpty(password)) {
            factory.setPassword(password);
        }
        if (!StringTools.isEmpty(port)) {
            try {
                factory.setPort(Integer.parseInt(port));
            } catch (NumberFormatException ignore) {

            }
        }
        if (!StringTools.isEmpty(virtualHost)) {
            factory.setVirtualHost(virtualHost);
        }
        factory.setHost(queueHost);

        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.queueDeclare(queueName, true, false, false, null);
        String message = convertToString(data);
        logger.info("Message text: " + message);
        channel.basicPublish("", queueName, MessageProperties.MINIMAL_PERSISTENT_BASIC, message.getBytes());
        logger.info("Message was sent");
    } catch (Exception ex) {
        logger.error("Uneble send message: " + convertToString(data));
        logger.debug(ex.getMessage(), ex);
    } finally {
        try {
            if (channel != null) {
                channel.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (Exception ignore) {

        }
    }
}

From source file:sd_aula06.Send.java

public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("reindeer.rmq.cloudamqp.com");
    factory.setUsername("jmodzuaw");
    factory.setPassword("Kwuy7kd81ED1fIj9gxEti1J4FTPBj2Jz");
    factory.setVirtualHost("jmodzuaw");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);

    String message = "RafaelReis: VSF!";
    channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
    System.out.println(" [x] Sent '" + message + "'");

    channel.close();//from   ww w. j  a  v  a2s.  c  om
    connection.close();
}

From source file:server.Worker.java

License:Open Source License

/**
 * @param args//from   w w  w .ja va 2s . c o m
 * @throws IOException
 * @throws TimeoutException
 */
public static void main(String[] args) throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection requestConnection = factory.newConnection();
    Channel requestChannel = requestConnection.createChannel();
    requestChannel.queueDeclare(REQUEST_QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Connection responseConnection = factory.newConnection();
    final Channel responseChannel = responseConnection.createChannel();
    responseChannel.queueDeclare(RESPONSE_QUEUE_NAME, false, false, false, null);

    Consumer consumer = new DefaultConsumer(requestChannel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            PriorityRequest request = (PriorityRequest) SerializationUtils.deserialize(body);
            System.out.println(" [x] Received");
            System.out.println(request.request.toString());
            System.out.println("*************************************");

            System.out.println(request.request.getMethod());
            HttpResponse response = PluginManager.getInstance().process(request.request, request.rootDir);
            if (response == null) {
                response = HttpResponseFactory.create400BadRequest(Protocol.CLOSE);
            }
            try {
                System.out.println(request.id);
            } catch (Exception e) {
                e.printStackTrace();
            }

            response.id = request.id;

            byte[] data = SerializationUtils.serialize(response);
            responseChannel.basicPublish("", RESPONSE_QUEUE_NAME, null, data);
        }
    };
    requestChannel.basicConsume(REQUEST_QUEUE_NAME, true, consumer);
}