List of usage examples for com.rabbitmq.client Channel basicConsume
String basicConsume(String queue, boolean autoAck, Consumer callback) throws IOException;
From source file:pubsub.RecieveLogs.java
public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, EXCHANGE_NAME, ""); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { @Override//from www . j a v a 2 s .com 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 + "'"); } }; channel.basicConsume(queueName, true, consumer); }
From source file:rabbitirc.RabbitIRC.java
public static void main(String[] argv) throws java.io.IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Random rnd = new Random(); int indeks = rnd.nextInt(usernamelist.length); String Randusername = usernamelist[indeks] + rnd.nextInt(100); user = Randusername;//from w w w. j ava 2 s . c o m channellist.add("lounge"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); String queueName = channel.queueDeclare().getQueue(); channel.exchangeDeclare(EXCHANGE_NAME, "direct"); Scanner sc = new Scanner(System.in); channel.queueBind(queueName, EXCHANGE_NAME, "lounge"); channellist.add("lounge"); 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 + "'"); } }; System.out.println(" Welcome " + user + " to Channel lounge"); System.out.println(" Queries: "); System.out.println(" 1. /NICK <Username> : Change username "); System.out.println(" 2. /JOIN <Channel Name> : Join Channel"); System.out.println(" 3. @<Channel Name> <Message> : Send message to Spesific Channel"); System.out.println(" 4. /LEAVE <Channel Name> : Leave Channel"); System.out.println(" 5. <Random text> : BroadCast"); System.out.println(" 6. /EXIT : Exit App"); while (true) { channel.basicConsume(queueName, true, consumer); String input = sc.nextLine(); String[] query; if ((query = CommandRegexes.NICK.match(input)) != null) { String Nickname = query[0]; user = Nickname; System.out.println(" [x] Your Nickname '" + Nickname + "'"); } else if ((query = CommandRegexes.JOIN.match(input)) != null) { String channel_name = query[0]; channel.queueBind(queueName, EXCHANGE_NAME, channel_name); channellist.add(channel_name); System.out.println(" [x] you had Join '" + channel_name + "'"); } else if ((query = CommandRegexes.LEAVE.match(input)) != null) { String channel_name = query[0]; if (channellist.contains(channel_name)) { channellist.remove(channellist.indexOf(channel_name)); channel.queueUnbind(queueName, EXCHANGE_NAME, channel_name); System.out.println(" [x] you had leave '" + channel_name); } else { System.out.println(" [x] you're not in the channel " + channel_name); } System.out.println(" [x] you had leave '" + channel_name + "'"); } else if (CommandRegexes.EXIT.match(input) != null) { channel.close(); connection.close(); break; } else if ((query = CommandRegexes.MESSAGE_CHANNEL.match(input)) != null) { String channel_name = query[0]; if (channellist.contains(channel_name)) { String message = "[" + channel_name + "] (" + user + ") " + query[1]; channel.basicPublish(EXCHANGE_NAME, channel_name, null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); System.out.println(" : to '" + channel_name + "'"); } else { System.out.println(" [x] No Channel " + channel_name + " on your list"); } } else { System.out.println(" [x] Broadcasting '" + input + "'"); for (String channellist1 : channellist) { String messages = "[" + channellist1 + "] (" + user + ") " + input; channel.basicPublish(EXCHANGE_NAME, channellist1, null, messages.getBytes()); } System.out.println(" [x] OK ;D"); } } }
From source file:rabbitmqapp.Recv.java
public static void main(String... args) throws Exception { ConnectionFactory factory = new ConnectionFactory(); String uri = "amqp://ytsoedex:Qu2LCiBJ5x9fhRUyLYkMhJqsURJ9dkSP@chicken.rmq.cloudamqp.com/ytsoedex"; factory.setUri(uri);/* w w w .j a v a 2 s . c o m*/ Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUENAME, false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { System.out.println(String.format("The consumer tag is %s", consumerTag)); ProductOrder message = (ProductOrder) RabbitUtility.getObject(body); //String message = new String(body, "UTF-8"); System.out.println(" [x] Received name'" + message.getName() + "'"); System.out.println(" [x] Received price'" + message.getPrice() + "'"); } }; channel.basicConsume(QUENAME, true, consumer); }
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 www .j a v a 2 s . co m*/ 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
@Test public void sendRetryOnFailureAllFluxMessagesShouldBeSentAndConsumed() throws Exception { int nbMessages = 10; CountDownLatch latch = new CountDownLatch(nbMessages); AtomicInteger counter = new AtomicInteger(); Channel channel = connection.createChannel(); channel.basicConsume(queue, true, new DefaultConsumer(channel) { @Override//w w w .j a va2s . com public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) { counter.incrementAndGet(); latch.countDown(); } }); Flux<OutboundMessage> msgFlux = Flux.range(0, nbMessages) .map(i -> new OutboundMessage("", queue, "".getBytes())).delayElements(ofMillis(200)); sender = createSender(new SenderOptions().connectionMono(connectionMono)); sender.send(msgFlux, new SendOptions().exceptionHandler(new ExceptionHandlers.RetrySendingExceptionHandler(ofSeconds(5), ofMillis(100), ExceptionHandlers.CONNECTION_RECOVERY_PREDICATE))) .subscribe(); closeAndWaitForRecovery((RecoverableConnection) connectionMono.block()); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertEquals(nbMessages, counter.get()); }
From source file:reactor.rabbitmq.RabbitFluxTests.java
License:Open Source License
@Test public void sender() throws Exception { int nbMessages = 10; CountDownLatch latch = new CountDownLatch(nbMessages); AtomicInteger counter = new AtomicInteger(); Channel channel = connection.createChannel(); channel.basicConsume(queue, true, new DefaultConsumer(channel) { @Override/*from w ww . java 2 s . c o m*/ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { counter.incrementAndGet(); latch.countDown(); } }); Flux<OutboundMessage> msgFlux = Flux.range(0, nbMessages) .map(i -> new OutboundMessage("", queue, "".getBytes())); sender = createSender(); sender.send(msgFlux).subscribe(); assertTrue(latch.await(1, TimeUnit.SECONDS)); assertEquals(nbMessages, counter.get()); }
From source file:reactor.rabbitmq.ReactorRabbitMqTests.java
License:Open Source License
@Test public void sender() throws Exception { int nbMessages = 10; CountDownLatch latch = new CountDownLatch(nbMessages); AtomicInteger counter = new AtomicInteger(); Channel channel = connection.createChannel(); channel.basicConsume(queue, true, new DefaultConsumer(channel) { @Override/*w ww . j a v a 2 s.c om*/ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { counter.incrementAndGet(); latch.countDown(); } }); Flux<OutboundMessage> msgFlux = Flux.range(0, nbMessages) .map(i -> new OutboundMessage("", queue, "".getBytes())); sender = ReactorRabbitMq.createSender(); sender.send(msgFlux).subscribe(); assertTrue(latch.await(1, TimeUnit.SECONDS)); assertEquals(nbMessages, counter.get()); }
From source file:reactor.rabbitmq.ReactorRabbitMqTests.java
License:Open Source License
@Test public void publishConfirms() throws Exception { int nbMessages = 10; CountDownLatch consumedLatch = new CountDownLatch(nbMessages); CountDownLatch confirmedLatch = new CountDownLatch(nbMessages); AtomicInteger counter = new AtomicInteger(); Channel channel = connection.createChannel(); channel.basicConsume(queue, true, new DefaultConsumer(channel) { @Override//from ww w. j a va2 s. c o m public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { counter.incrementAndGet(); consumedLatch.countDown(); } }); Flux<OutboundMessage> msgFlux = Flux.range(0, nbMessages) .map(i -> new OutboundMessage("", queue, "".getBytes())); sender = ReactorRabbitMq.createSender(); sender.sendWithPublishConfirms(msgFlux).subscribe(outboundMessageResult -> { confirmedLatch.countDown(); }); assertTrue(consumedLatch.await(1, TimeUnit.SECONDS)); assertTrue(confirmedLatch.await(1, TimeUnit.SECONDS)); assertEquals(nbMessages, counter.get()); }
From source file:reactor.rabbitmq.Receiver.java
License:Open Source License
public Flux<Delivery> consumeNoAck(final String queue, ReceiverOptions options) { // TODO track flux so it can be disposed when the sender is closed? // could be also developer responsibility return Flux.create(emitter -> { connectionMono.subscribe(connection -> { try { // TODO handle exception Channel channel = connection.createChannel(); final DefaultConsumer consumer = new DefaultConsumer(channel) { @Override/*from w w w . j ava2 s.c o m*/ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { emitter.next(new Delivery(envelope, properties, body)); } @Override public void handleCancel(String consumerTag) throws IOException { LOGGER.warn("Flux consumer {} has been cancelled", consumerTag); } }; final String consumerTag = channel.basicConsume(queue, true, consumer); emitter.onDispose(() -> { try { if (channel.isOpen() && channel.getConnection().isOpen()) { channel.basicCancel(consumerTag); channel.close(); } } catch (TimeoutException | IOException e) { throw new ReactorRabbitMqException(e); } }); } catch (IOException e) { throw new ReactorRabbitMqException(e); } }); }, options.getOverflowStrategy()); }
From source file:reactor.rabbitmq.Receiver.java
License:Open Source License
public Flux<AcknowledgableDelivery> consumeManuelAck(final String queue, ReceiverOptions options) { // TODO track flux so it can be disposed when the sender is closed? // could be also developer responsibility return Flux.create(emitter -> { connectionMono.subscribe(connection -> { try { Channel channel = connection.createChannel(); if (options.getQos() != 0) { channel.basicQos(options.getQos()); }//from w w w .ja va 2 s .c o m final DefaultConsumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { AcknowledgableDelivery message = new AcknowledgableDelivery(envelope, properties, body, getChannel()); if (options.getHookBeforeEmit().apply(emitter, message)) { emitter.next(message); } } }; final String consumerTag = channel.basicConsume(queue, false, consumer); emitter.onDispose(() -> { try { if (channel.isOpen() && channel.getConnection().isOpen()) { channel.basicCancel(consumerTag); channel.close(); } } catch (TimeoutException | IOException e) { throw new ReactorRabbitMqException(e); } }); } catch (IOException e) { throw new ReactorRabbitMqException(e); } }); }, options.getOverflowStrategy()); }