/** * Creates a new {@link ExchangePublisher}. The Publisher accepts strings and sends them as a text message. */ public ExchangePublisher<String> createExchangeTextPublisher(final String name) { Preconditions.checkState(connectionFactory != null, "connection factory was never injected!"); return new ExchangePublisher<String>(connectionFactory, amqpConfig, name, stringPublisherCallback); }
@Override protected void publish(final PublisherData publisherData) throws IOException { final Channel channel = channelConnect(); // An exchange has its own name and the default routing key... channel.basicPublish(getName(), routingKey, publisherData.getProperties(), publisherData.getData()); } }
Assert.assertFalse(topicProducer1.isConnected()); Assert.assertFalse(topicProducer2.isConnected()); topicProducer1.put(format("hello, world %d", i)); topicProducer2.put(format("hello, wold %d", i)); Assert.assertTrue(topicProducer1.isEmpty()); Assert.assertTrue(topicProducer2.isEmpty()); Assert.assertEquals(maxCount*2, cmc.getCount()); topicProducer1.shutdown(); topicProducer2.shutdown(); topicConsumer.shutdown(); producerThread1.interrupt();
Assert.assertFalse(topicProducer.isConnected()); if (!topicProducer.offerWithTimeout("dummy", 50L, TimeUnit.MILLISECONDS)) { break; Assert.assertFalse(topicProducer.isConnected()); Assert.assertTrue(topicConsumer.isConnected()); topicProducer.shutdown(); topicConsumer.shutdown(); producerThread.interrupt();
@Test public void stopProducerWithNoMessages() throws Exception { final ExchangePublisher<Object>topicProducer = exchangeRunnableFactory.createExchangeJsonPublisher("test-topic"); Assert.assertNotNull(topicProducer); final Thread topicThread = new Thread(topicProducer); topicThread.start(); Thread.sleep(2000L); Assert.assertFalse(topicProducer.isConnected()); topicProducer.shutdown(); topicThread.interrupt(); topicThread.join(); }
@Test public void testProduceConsume() throws Exception { final CountingMessageCallback cmc = new CountingMessageCallback(); final ExchangeConsumer topicConsumer = exchangeRunnableFactory.createExchangeListener("test-topic", cmc); final ExchangePublisher<Object> topicProducer = exchangeRunnableFactory.createExchangeJsonPublisher("test-topic"); final Thread consumerThread = new Thread(topicConsumer); final Thread producerThread = new Thread(topicProducer); consumerThread.start(); producerThread.start(); Thread.sleep(1000L); Assert.assertTrue(topicConsumer.isConnected()); Assert.assertFalse(topicProducer.isConnected()); final int maxCount = 1000; for (int i = 0; i < maxCount; i++) { topicProducer.put(format("hello, world %d", i)); } Thread.sleep(DRAIN_SLEEP); Assert.assertTrue(topicProducer.isEmpty()); Assert.assertEquals(maxCount, cmc.getCount()); topicProducer.shutdown(); topicConsumer.shutdown(); producerThread.interrupt(); consumerThread.interrupt(); producerThread.join(); consumerThread.join(); }
Assert.assertFalse(exchangeProducer.isConnected()); Assert.assertFalse(exchangeProducer.isConnected()); exchangeProducer.shutdown(); exchangeConsumer.shutdown(); producerThread.interrupt();
Assert.assertFalse(topicProducer.isConnected()); topicProducer.put("dummy"); Assert.assertTrue(topicProducer.isEmpty()); Assert.assertTrue(topicProducer.isConnected()); Assert.assertFalse(topicConsumer.isConnected()); topicProducer.shutdown(); topicConsumer.shutdown(); producerThread.interrupt();
/** * Creates a new {@link ExchangePublisher}. The Publisher accepts arbitrary objects and uses the Jackson object mapper to convert them into * JSON and sends them as a text message. */ public <T> ExchangePublisher<T> createExchangeJsonPublisher(final String name) { Preconditions.checkState(connectionFactory != null, "connection factory was never injected!"); return new ExchangePublisher<T>(connectionFactory, amqpConfig, name, jsonPublisherCallback); }
Assert.assertFalse(topicProducer.isConnected()); topicProducer.put("dummy"); if (i > 500) { consumerThread.interrupt(); Assert.assertTrue(topicProducer.isEmpty()); Assert.assertTrue(topicProducer.isConnected()); Assert.assertFalse(topicConsumer.isConnected()); topicProducer.shutdown(); topicConsumer.shutdown(); producerThread.interrupt();
/** * Creates a new {@link ExchangePublisher}. The callback is called to convert an object that is sent into the ExchangePublisher * into an AMQP byte array.. */ public <T> ExchangePublisher<T> createExchangePublisher(final String name, final PublisherCallback<T> messageCallback) { Preconditions.checkState(connectionFactory != null, "connection factory was never injected!"); return new ExchangePublisher<T>(connectionFactory, amqpConfig, name, messageCallback); }
Assert.assertFalse(topicProducer1.isConnected()); Assert.assertFalse(topicProducer2.isConnected()); topicProducer1.put(format("hello, world %d", i)); topicProducer2.put(format("hello, wold %d", i)); Assert.assertTrue(topicProducer1.isEmpty()); Assert.assertTrue(topicProducer2.isEmpty()); Assert.assertEquals(maxCount*2, cmc2.getCount()); topicProducer1.shutdown(); topicProducer2.shutdown(); topicConsumer1.shutdown(); topicConsumer2.shutdown();
Assert.assertFalse(topicProducer.isConnected()); topicProducer.put(format("hello, world %d", i)); Assert.assertTrue(topicProducer.isEmpty()); Assert.assertEquals(maxCount, cmc1.getCount()); Assert.assertEquals(maxCount, cmc2.getCount()); topicProducer.shutdown(); topicConsumer1.shutdown(); topicConsumer2.shutdown();