@Override public ConnectionFactory get() { final URI amqpUri = amqpConfig.getAmqpConnectionUrl(); try { final ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setUri(amqpUri); return connectionFactory; } catch (Exception e) { Throwables.propagateIfPossible(e); } return null; } }
private void backoff(final Throwable t) throws InterruptedException { final long backoffTime = amqpConfig.getBackoffDelay().getMillis() * backoff; LOG.warnDebug(t, "Could not connect to Broker, sleeping for %d ms...", backoffTime); Thread.sleep(backoffTime); if (backoff != 1 << amqpConfig.getMaxBackoffFactor()) { backoff <<= 1; } channelDisconnect(); }
@Override protected void connectCallback(@Nonnull final Channel channel) throws IOException { if (getConfig().isDeclaring()) { channel.exchangeDeclare(getName(), getConfig().getExchangeType(), getConfig().isDurable(), getConfig().isAutoDelete(), null); } }
protected AbstractPublisher(@Nonnull final ConnectionFactory connectionFactory, @Nonnull final AmqpConfig amqpConfig, @Nonnull final String name, @Nonnull final PublisherCallback<? super T> publisherCallback) { super(connectionFactory, amqpConfig, name); this.publisherCallback = publisherCallback; this.messageQueue = new ArrayBlockingQueue<T>(amqpConfig.getPublisherQueueLength()); this.transmitTimeout = amqpConfig.getTransmitTimeout(); this.tickTimeout = amqpConfig.getTickTimeout(); }
@Override protected void connectCallback(@Nonnull final Channel channel) throws IOException { if (getConfig().isDeclaring()) { channel.queueDeclare(getName(), getConfig().isDurable(), getConfig().isExclusive(), getConfig().isAutoDelete(), null); } }
@Override protected void configure() { final Named connectionNamed; final AmqpConfig amqpConfig; connectionNamed = Names.named(connectionName); bind(new TypeLiteral<PublisherCallback<Object>>() {}).annotatedWith(JsonMapper.class).to(JsonPublisherCallback.class).in(Scopes.SINGLETON); bind(new TypeLiteral<PublisherCallback<String>>() {}).to(StringPublisherCallback.class).in(Scopes.SINGLETON); amqpConfig = config.getBean(AmqpConfig.class, ImmutableMap.of("name", connectionName)); bind(AmqpConfig.class).annotatedWith(connectionNamed).toInstance(amqpConfig); if (amqpConfig.isEnabled()) { LOG.info("Enabling AMQP for '%s'", connectionName); bind(ConnectionFactory.class).annotatedWith(connectionNamed).toProvider(new AmqpFactoryProvider(amqpConfig)).in(Scopes.SINGLETON); bind(AmqpRunnableFactory.class).annotatedWith(connectionNamed).toInstance(new AmqpRunnableFactory(connectionNamed)); } else { LOG.info("Disabled AMQP for '%s'", connectionName); } }
public ExchangePublisher(@Nonnull final ConnectionFactory connectionFactory, @Nonnull final AmqpConfig amqpConfig, @Nonnull final String name, @Nonnull final PublisherCallback<? super T> publisherCallback) { super(connectionFactory, amqpConfig, name, publisherCallback); this.routingKey = amqpConfig.getRoutingKey(); }
protected AbstractConsumer(@Nonnull final ConnectionFactory connectionFactory, @Nonnull final AmqpConfig amqpConfig, @Nonnull final String name, @Nonnull final ConsumerCallback consumerCallback) { super(connectionFactory, amqpConfig, name); this.consumerCallback = consumerCallback; this.tickTimeout = getConfig().getTickTimeout().getMillis(); }
@Override protected void connectCallback(final Channel channel) throws IOException { super.connectCallback(channel); if (getConfig().isDeclaring()) { channel.queueDeclare(getName(), getConfig().isDurable(), getConfig().isExclusive(), getConfig().isAutoDelete(), null); } channel.basicConsume(getName(), false, getConsumer()); } }
@Test public void testNamedWorksWithLocalUri() { final Config config = Config.getFixedConfig(ImmutableMap.of("ness.amqp.test.enabled", "true", "ness.amqp.test.connection-url", "amqp://unknown-host/")); final Injector injector = Guice.createInjector(Stage.PRODUCTION, new ConfigModule(config), new NessJacksonModule(), new AmqpModule(config, "test")); final AmqpConfig amqpConfig = injector.getInstance(Key.get(AmqpConfig.class, Names.named("test"))); Assert.assertNotNull(amqpConfig); Assert.assertTrue(amqpConfig.isEnabled()); final ConnectionFactory factory = injector.getInstance(Key.get(ConnectionFactory.class, Names.named("test"))); Assert.assertNotNull(factory); }
public ExchangeConsumer(@Nonnull final ConnectionFactory connectionFactory, @Nonnull final AmqpConfig amqpConfig, @Nonnull final String name, @Nonnull final ConsumerCallback consumerCallback) { super(connectionFactory, amqpConfig, name, consumerCallback); this.routingKey = amqpConfig.getRoutingKey(); }
@Override protected void connectCallback(@Nonnull final Channel channel) throws IOException { super.connectCallback(channel); if (getConfig().isDeclaring()) { channel.exchangeDeclare(getName(), getConfig().getExchangeType(), getConfig().isDurable(), getConfig().isAutoDelete(), null); } final String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, getName(), routingKey); channel.basicConsume(queueName, false, getConsumer()); } }
@Test public void testNamedWorksWithGlobalUri() { final Config config = Config.getFixedConfig(ImmutableMap.of("ness.amqp.test.enabled", "true", "ness.amqp.connection-url", "amqp://unknown-host/")); final Injector injector = Guice.createInjector(Stage.PRODUCTION, new ConfigModule(config), new NessJacksonModule(), new AmqpModule(config, "test")); final AmqpConfig amqpConfig = injector.getInstance(Key.get(AmqpConfig.class, Names.named("test"))); Assert.assertNotNull(amqpConfig); Assert.assertTrue(amqpConfig.isEnabled()); final ConnectionFactory factory = injector.getInstance(Key.get(ConnectionFactory.class, Names.named("test"))); Assert.assertNotNull(factory); }
@Test public void testNamedWorksGlobalDisabled() { final Config config = Config.getFixedConfig(ImmutableMap.of("ness.amqp.enabled", "false")); final Injector injector = Guice.createInjector(Stage.PRODUCTION, new ConfigModule(config), new NessJacksonModule(), new AmqpModule(config, "test")); final AmqpConfig amqpConfig = injector.getInstance(Key.get(AmqpConfig.class, Names.named("test"))); Assert.assertNotNull(amqpConfig); Assert.assertFalse(amqpConfig.isEnabled()); Assert.assertNull(injector.getExistingBinding(Key.get(ConnectionFactory.class, Names.named("test")))); }
@Test public void testMultipleModules() { final Config config = Config.getFixedConfig(ImmutableMap.of("ness.amqp.enabled", "true", "ness.amqp.connection-url", "amqp://unknown-host/")); final Injector injector = Guice.createInjector(Stage.PRODUCTION, new ConfigModule(config), new NessJacksonModule(), new AmqpModule(config, "test"), new AmqpModule(config, "test2")); final AmqpConfig AmqpConfig = injector.getInstance(Key.get(AmqpConfig.class, Names.named("test2"))); Assert.assertNotNull(AmqpConfig); Assert.assertTrue(AmqpConfig.isEnabled()); final ConnectionFactory factory = injector.getInstance(Key.get(ConnectionFactory.class, Names.named("test2"))); Assert.assertNotNull(factory); Assert.assertNotNull(injector.getInstance(Key.get(ConnectionFactory.class, Names.named("test")))); } }
@Test public void testNamedWorksLocalDisabled() { final Config config = Config.getFixedConfig(ImmutableMap.of("ness.amqp.enabled", "true", "ness.amqp.test.enabled", "false")); final Injector injector = Guice.createInjector(Stage.PRODUCTION, new ConfigModule(config), new NessJacksonModule(), new AmqpModule(config, "test")); final AmqpConfig amqpConfig = injector.getInstance(Key.get(AmqpConfig.class, Names.named("test"))); Assert.assertNotNull(amqpConfig); Assert.assertFalse(amqpConfig.isEnabled()); Assert.assertNull(injector.getExistingBinding(Key.get(ConnectionFactory.class, Names.named("test")))); }