org.springframework.amqp.rabbit.connection.CachingConnectionFactory Java Examples
The following examples show how to use
org.springframework.amqp.rabbit.connection.CachingConnectionFactory.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RabbitMQConfiguration.java From cukes with Apache License 2.0 | 8 votes |
@Bean @DependsOn("amqpBroker") CachingConnectionFactory connectionFactory() { log.info("Creating connection factory for MQ broker..."); ConnectionFactory cf = new ConnectionFactory(); cf.setHost(hostname); cf.setPort(port); cf.setUsername(userName); cf.setPassword(password); cf.setVirtualHost(virtualHost); cf.setAutomaticRecoveryEnabled(false); if (useSSL) { try { cf.useSslProtocol(); } catch (NoSuchAlgorithmException | KeyManagementException e) { //TODO don't throw raw exceptions throw new RuntimeException(e); } } log.info("Connection factory created."); return new CachingConnectionFactory(cf); }
Example #2
Source File: RabbitConnectionFactoryCreator.java From spring-cloud-connectors with Apache License 2.0 | 6 votes |
private CachingConnectionFactory createSpringConnectionFactory(AmqpServiceInfo serviceInfo, ServiceConnectorConfig serviceConnectorConfiguration, com.rabbitmq.client.ConnectionFactory connectionFactory) { CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(connectionFactory); if (serviceInfo.getUris() != null) { cachingConnectionFactory.setAddresses(getAddresses(serviceInfo)); } if (serviceConnectorConfiguration != null) { Integer channelCacheSize = ((RabbitConnectionFactoryConfig) serviceConnectorConfiguration).getChannelCacheSize(); if (channelCacheSize != null) { cachingConnectionFactory.setChannelCacheSize(channelCacheSize); } } return cachingConnectionFactory; }
Example #3
Source File: RabbitServiceAutoConfiguration.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 6 votes |
/** * Creates a {@link ConnectionFactory} using the singleton service * connector. * @param cloud {@link Cloud} instance to be used for accessing services. * @param connectorConfigObjectProvider the {@link ObjectProvider} for the * {@link RabbitConnectionFactoryConfig}. * @param applicationContext application context instance * @param rabbitProperties rabbit properties * @return the {@link ConnectionFactory} used by the binder. * @throws Exception if configuration of connection factory fails */ @Bean @Primary ConnectionFactory rabbitConnectionFactory(Cloud cloud, ObjectProvider<RabbitConnectionFactoryConfig> connectorConfigObjectProvider, ConfigurableApplicationContext applicationContext, RabbitProperties rabbitProperties) throws Exception { ConnectionFactory connectionFactory = cloud .getSingletonServiceConnector(ConnectionFactory.class, connectorConfigObjectProvider.getIfUnique()); configureCachingConnectionFactory( (CachingConnectionFactory) connectionFactory, applicationContext, rabbitProperties); return connectionFactory; }
Example #4
Source File: RabbitMessageChannelBinder.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 6 votes |
private void checkConnectionFactoryIsErrorCapable() { if (!(this.connectionFactory instanceof CachingConnectionFactory)) { logger.warn( "Unknown connection factory type, cannot determine error capabilities: " + this.connectionFactory.getClass()); } else { CachingConnectionFactory ccf = (CachingConnectionFactory) this.connectionFactory; if (!ccf.isPublisherConfirms() && !ccf.isPublisherReturns()) { logger.warn( "Producer error channel is enabled, but the connection factory is not configured for " + "returns or confirms; the error channel will receive no messages"); } else if (!ccf.isPublisherConfirms()) { logger.info( "Producer error channel is enabled, but the connection factory is only configured to " + "handle returned messages; negative acks will not be reported"); } else if (!ccf.isPublisherReturns()) { logger.info( "Producer error channel is enabled, but the connection factory is only configured to " + "handle negatively acked messages; returned messages will not be reported"); } } }
Example #5
Source File: AmqpTestConfiguration.java From hawkbit with Eclipse Public License 1.0 | 6 votes |
@Bean ConnectionFactory rabbitConnectionFactory(final RabbitMqSetupService rabbitmqSetupService) { final CachingConnectionFactory factory = new CachingConnectionFactory(); factory.setHost(rabbitmqSetupService.getHostname()); factory.setPort(5672); factory.setUsername(rabbitmqSetupService.getUsername()); factory.setPassword(rabbitmqSetupService.getPassword()); try { factory.setVirtualHost(rabbitmqSetupService.createVirtualHost()); // All exception are catched. The BrokerRunning decide if the // test should break or not } catch (@SuppressWarnings("squid:S2221") final Exception e) { Throwables.propagateIfInstanceOf(e, AlivenessException.class); LOG.error("Cannot create virtual host.", e); } return factory; }
Example #6
Source File: EMSConfiguration.java From generic-vnfm with Apache License 2.0 | 6 votes |
@PostConstruct private void init() { log.info("Initialization of RabbitConfiguration"); emsConnectionFactory = new CachingConnectionFactory(); ((CachingConnectionFactory) emsConnectionFactory).setHost(brokerIp); ((CachingConnectionFactory) emsConnectionFactory).setPort(rabbitPort); ((CachingConnectionFactory) emsConnectionFactory).setUsername(emsRabbitUsername); ((CachingConnectionFactory) emsConnectionFactory).setPassword(emsRabbitPassword); rabbitAdmin = new RabbitAdmin(emsConnectionFactory); TopicExchange topicExchange = new TopicExchange("openbaton-exchange"); rabbitAdmin.declareExchange(topicExchange); log.info("exchange declared"); queueName_emsRegistrator = "ems." + vnfmHelper.getVnfmEndpoint() + ".register"; rabbitAdmin.declareQueue(new Queue(queueName_emsRegistrator, durable, exclusive, autodelete)); }
Example #7
Source File: RabbitBinderTests.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 6 votes |
@Test public void testMultiplexOnPartitionedConsumer() throws Exception { final ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties(); RabbitTestSupport.RabbitProxy proxy = new RabbitTestSupport.RabbitProxy(); CachingConnectionFactory cf = new CachingConnectionFactory("localhost", proxy.getPort()); final RabbitExchangeQueueProvisioner rabbitExchangeQueueProvisioner = new RabbitExchangeQueueProvisioner(cf); consumerProperties.setMultiplex(true); consumerProperties.setPartitioned(true); consumerProperties.setInstanceIndexList(Arrays.asList(1, 2, 3)); final ConsumerDestination consumerDestination = rabbitExchangeQueueProvisioner.provisionConsumerDestination("foo", "boo", consumerProperties); final String name = consumerDestination.getName(); assertThat(name).isEqualTo("foo.boo-1,foo.boo-2,foo.boo-3"); }
Example #8
Source File: RabbitBinderTests.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 6 votes |
@Test public void testMultiplexOnPartitionedConsumerWithMultipleDestinations() throws Exception { final ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties(); RabbitTestSupport.RabbitProxy proxy = new RabbitTestSupport.RabbitProxy(); CachingConnectionFactory cf = new CachingConnectionFactory("localhost", proxy.getPort()); final RabbitExchangeQueueProvisioner rabbitExchangeQueueProvisioner = new RabbitExchangeQueueProvisioner(cf); consumerProperties.setMultiplex(true); consumerProperties.setPartitioned(true); consumerProperties.setInstanceIndexList(Arrays.asList(1, 2, 3)); final ConsumerDestination consumerDestination = rabbitExchangeQueueProvisioner.provisionConsumerDestination("foo,qaa", "boo", consumerProperties); final String name = consumerDestination.getName(); assertThat(name).isEqualTo("foo.boo-1,foo.boo-2,foo.boo-3,qaa.boo-1,qaa.boo-2,qaa.boo-3"); }
Example #9
Source File: EventRegistryRabbitConfiguration.java From flowable-engine with Apache License 2.0 | 6 votes |
@Bean public CachingConnectionFactory rabbitConnectionFactory(RabbitMQContainer rabbitMQContainer) throws Exception { // configuration properties are Spring Boot defaults RabbitConnectionFactoryBean rabbitFactory = new RabbitConnectionFactoryBean(); rabbitFactory.setPort(rabbitMQContainer.getAmqpPort()); rabbitFactory.afterPropertiesSet(); CachingConnectionFactory factory = new CachingConnectionFactory(rabbitFactory.getObject()); return factory; }
Example #10
Source File: TurbineApp.java From Mastering-Microservices-with-Java with MIT License | 5 votes |
@Bean public ConnectionFactory connectionFactory() { //LOG.info("Creating RabbitMQHost ConnectionFactory for host: {}", rabbitMQHost); CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(rabbitMQHost); return cachingConnectionFactory; }
Example #11
Source File: TurbineApp.java From Microservices-Building-Scalable-Software with MIT License | 5 votes |
@Bean public ConnectionFactory connectionFactory() { //LOG.info("Creating RabbitMQHost ConnectionFactory for host: {}", rabbitMQHost); CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(rabbitMQHost); return cachingConnectionFactory; }
Example #12
Source File: RabbitTestSupport.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 5 votes |
@Override protected void obtainResource() throws Exception { resource = new CachingConnectionFactory("localhost"); resource.createConnection().close(); if (management) { Socket socket = SocketFactory.getDefault().createSocket("localhost", 15672); socket.close(); } }
Example #13
Source File: RabbitServiceAutoConfiguration.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 5 votes |
static void configureCachingConnectionFactory( CachingConnectionFactory connectionFactory, ConfigurableApplicationContext applicationContext, RabbitProperties rabbitProperties) throws Exception { if (StringUtils.hasText(rabbitProperties.getAddresses())) { connectionFactory.setAddresses(rabbitProperties.determineAddresses()); } connectionFactory.setPublisherConfirmType(rabbitProperties.getPublisherConfirmType() == null ? ConfirmType.NONE : rabbitProperties.getPublisherConfirmType()); connectionFactory.setPublisherReturns(rabbitProperties.isPublisherReturns()); if (rabbitProperties.getCache().getChannel().getSize() != null) { connectionFactory.setChannelCacheSize( rabbitProperties.getCache().getChannel().getSize()); } if (rabbitProperties.getCache().getConnection().getMode() != null) { connectionFactory .setCacheMode(rabbitProperties.getCache().getConnection().getMode()); } if (rabbitProperties.getCache().getConnection().getSize() != null) { connectionFactory.setConnectionCacheSize( rabbitProperties.getCache().getConnection().getSize()); } if (rabbitProperties.getCache().getChannel().getCheckoutTimeout() != null) { connectionFactory.setChannelCheckoutTimeout(rabbitProperties.getCache() .getChannel().getCheckoutTimeout().toMillis()); } connectionFactory.setApplicationContext(applicationContext); applicationContext.addApplicationListener(connectionFactory); connectionFactory.afterPropertiesSet(); }
Example #14
Source File: RabbitMessageChannelBinderConfiguration.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 5 votes |
@Bean @ConditionalOnMissingBean(ConnectionNameStrategy.class) @ConditionalOnProperty("spring.cloud.stream.rabbit.binder.connection-name-prefix") public ConnectionNameStrategy connectionNamer(CachingConnectionFactory cf) { final AtomicInteger nameIncrementer = new AtomicInteger(); ConnectionNameStrategy namer = f -> this.rabbitBinderConfigurationProperties .getConnectionNamePrefix() + "#" + nameIncrementer.getAndIncrement(); // TODO: this can be removed when Boot 2.0.1 wires it in cf.setConnectionNameStrategy(namer); return namer; }
Example #15
Source File: LocalizedQueueConnectionFactoryIntegrationTests.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 5 votes |
@Test public void testConnect() { RabbitAdmin admin = new RabbitAdmin(this.lqcf); Queue queue = new Queue(UUID.randomUUID().toString(), false, false, true); admin.declareQueue(queue); ConnectionFactory targetConnectionFactory = this.lqcf .getTargetConnectionFactory("[" + queue.getName() + "]"); RabbitTemplate template = new RabbitTemplate(targetConnectionFactory); template.convertAndSend("", queue.getName(), "foo"); assertThat(template.receiveAndConvert(queue.getName())).isEqualTo("foo"); ((CachingConnectionFactory) targetConnectionFactory).destroy(); admin.deleteQueue(queue.getName()); }
Example #16
Source File: RabbitBinderTests.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 5 votes |
@Test public void testProducerAckChannel() throws Exception { RabbitTestBinder binder = getBinder(); CachingConnectionFactory ccf = this.rabbitAvailableRule.getResource(); ccf.setPublisherReturns(true); ccf.setPublisherConfirms(true); ccf.resetConnection(); DirectChannel moduleOutputChannel = createBindableChannel("output", new BindingProperties()); ExtendedProducerProperties<RabbitProducerProperties> producerProps = createProducerProperties(); producerProps.setErrorChannelEnabled(true); producerProps.getExtension().setConfirmAckChannel("acksChannel"); Binding<MessageChannel> producerBinding = binder.bindProducer("acks.0", moduleOutputChannel, producerProps); final Message<?> message = MessageBuilder.withPayload("acksMessage".getBytes()) .build(); final AtomicReference<Message<?>> confirm = new AtomicReference<>(); final CountDownLatch confirmLatch = new CountDownLatch(1); binder.getApplicationContext().getBean("acksChannel", DirectChannel.class) .subscribe(m -> { confirm.set(m); confirmLatch.countDown(); }); moduleOutputChannel.send(message); assertThat(confirmLatch.await(10, TimeUnit.SECONDS)).isTrue(); assertThat(confirm.get().getPayload()).isEqualTo("acksMessage".getBytes()); producerBinding.unbind(); }
Example #17
Source File: TurbineApp.java From Mastering-Microservices-with-Java with MIT License | 5 votes |
@Bean public ConnectionFactory connectionFactory() { //LOG.info("Creating RabbitMQHost ConnectionFactory for host: {}", rabbitMQHost); CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(rabbitMQHost); return cachingConnectionFactory; }
Example #18
Source File: ITSpringRabbit.java From brave with Apache License 2.0 | 5 votes |
@Before public void refresh() { producerContext.registerBean(SpringRabbitTracing.class, () -> producerTracing); producerContext.registerBean(CachingConnectionFactory.class, () -> connectionFactory); producerContext.registerBean(Binding.class, () -> binding); producerContext.register(RabbitProducerConfig.class); producerContext.refresh(); consumerContext.registerBean(SpringRabbitTracing.class, () -> consumerTracing); consumerContext.registerBean(CachingConnectionFactory.class, () -> connectionFactory); consumerContext.registerBean(Binding.class, () -> binding); consumerContext.register(RabbitConsumerConfig.class); consumerContext.refresh(); }
Example #19
Source File: TurbineApp.java From Mastering-Microservices-with-Java with MIT License | 5 votes |
@Bean public ConnectionFactory connectionFactory() { //LOG.info("Creating RabbitMQHost ConnectionFactory for host: {}", rabbitMQHost); CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(rabbitMQHost); return cachingConnectionFactory; }
Example #20
Source File: Registration.java From NFVO with Apache License 2.0 | 5 votes |
/** * This method registers a Vnfm to the Nfvo by sending a request to the nfvo.manager.handling * queue using the given RabbitTemplate. The Nfvo's answer contains a username and password which * is injected into the RabbitTemplate so that it can send requests to the queue dedicated to the * specific Vnfm possessing the RabbitTemplate object. * * @param rabbitTemplate The spring RabbitTemplate @see * org.springframework.amqp.rabbit.core.RabbitTemplate * @param endpoint The VNFM Endpoint to register @see * org.openbaton.catalogue.nfvo.VnfmManagerEndpoint * @return new username and password for rabbitmq for specific vnfm endpoint */ public String[] registerVnfmToNfvo(RabbitTemplate rabbitTemplate, VnfmManagerEndpoint endpoint) { JsonObject message = new JsonObject(); message.add("type", new JsonPrimitive(endpoint.getType())); message.add("action", new JsonPrimitive("register")); message.add("vnfmManagerEndpoint", gson.toJsonTree(endpoint, VnfmManagerEndpoint.class)); log.debug("Registering the Vnfm to the Nfvo"); Object res = rabbitTemplate.convertSendAndReceive( "openbaton-exchange", "nfvo.manager.handling", gson.toJson(message)); if (res == null) { throw new IllegalArgumentException("The NFVO's answer to the registration request is null."); } if (!(res instanceof String)) { throw new IllegalArgumentException( "The NFVO's answer to the registration request should be of type String, but it is " + res.getClass().getSimpleName()); } ManagerCredentials managerCredentials = gson.fromJson((String) res, ManagerCredentials.class); this.username = managerCredentials.getRabbitUsername(); this.password = managerCredentials.getRabbitPassword(); ((CachingConnectionFactory) rabbitTemplate.getConnectionFactory()).setUsername(username); ((CachingConnectionFactory) rabbitTemplate.getConnectionFactory()).setPassword(password); String[] usernamePassword = new String[2]; usernamePassword[0] = username; usernamePassword[1] = password; return usernamePassword; }
Example #21
Source File: ZipkinRabbitSenderConfiguration.java From spring-cloud-sleuth with Apache License 2.0 | 5 votes |
@Bean(ZipkinAutoConfiguration.SENDER_BEAN_NAME) Sender rabbitSender(CachingConnectionFactory connectionFactory, RabbitProperties config) { String addresses = StringUtils.hasText(this.addresses) ? this.addresses : config.determineAddresses(); return RabbitMQSender.newBuilder() .connectionFactory(connectionFactory.getRabbitConnectionFactory()) .queue(this.queue).addresses(addresses).build(); }
Example #22
Source File: MqBeanParser.java From zxl with Apache License 2.0 | 5 votes |
protected void buildConnectionFactoryBeanDefinition(String beanName, String host, BeanDefinitionRegistry beanDefinitionRegistry) { AbstractBeanDefinition connectionFactoryBeanDefinition = new GenericBeanDefinition(); connectionFactoryBeanDefinition.setBeanClass(CachingConnectionFactory.class); if (!StringUtil.isEmpty(host)) { MutablePropertyValues connectionFactoryPropertyValues = new MutablePropertyValues(); connectionFactoryPropertyValues.add("host", host); connectionFactoryBeanDefinition.setPropertyValues(connectionFactoryPropertyValues); } beanDefinitionRegistry.registerBeanDefinition(beanName, connectionFactoryBeanDefinition); }
Example #23
Source File: AmqpContextConfig.java From sinavi-jfw with Apache License 2.0 | 5 votes |
/** * {@link ConnectionFactory}のインスタンスをDIコンテナに登録します。 * @return {@link CachingConnectionFactory}のインスタンス */ @Bean public ConnectionFactory factory() { CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, Integer.valueOf(port)); connectionFactory.setUsername(username); connectionFactory.setPassword(password); connectionFactory.setChannelCacheSize(channelCacheSize); configure(connectionFactory); return connectionFactory; }
Example #24
Source File: RabbitMQInitializerTest.java From sinavi-jfw with Apache License 2.0 | 5 votes |
@Bean public ConnectionFactory factory() { CachingConnectionFactory connectionFactory = new CachingConnectionFactory("127.0.0.1"); connectionFactory.setUsername("guest"); connectionFactory.setPassword("guest"); return connectionFactory; }
Example #25
Source File: RabbitMQLocalConfig.java From bearchoke with Apache License 2.0 | 5 votes |
/** * Bean is our direct connection to RabbitMQ * @return CachingConnectionFactory */ @Bean(destroyMethod = "destroy") public ConnectionFactory rabbitConnectionFactory() { CachingConnectionFactory factory = new CachingConnectionFactory( environment.getProperty("rabbitmq.host"), environment.getProperty("rabbitmq.port", Integer.class) ); factory.setUsername(environment.getProperty("rabbitmq.username")); factory.setPassword(environment.getProperty("rabbitmq.password")); return factory; }
Example #26
Source File: ITSpringRabbit.java From brave with Apache License 2.0 | 5 votes |
@BeforeClass public static void startRabbit() { rabbit.start(); CachingConnectionFactory connectionFactory = new CachingConnectionFactory(rabbit.getContainerIpAddress(), rabbit.getAmqpPort()); try { RabbitAdmin amqpAdmin = new RabbitAdmin(connectionFactory); amqpAdmin.declareExchange(exchange); amqpAdmin.declareQueue(queue); amqpAdmin.declareBinding(binding); } finally { connectionFactory.destroy(); } }
Example #27
Source File: TurbineApp.java From Microservices-Building-Scalable-Software with MIT License | 5 votes |
@Bean public ConnectionFactory connectionFactory() { //LOG.info("Creating RabbitMQHost ConnectionFactory for host: {}", rabbitMQHost); CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(rabbitMQHost); return cachingConnectionFactory; }
Example #28
Source File: RabbitMQConfiguration.java From Learning-Path-Spring-5-End-to-End-Programming with MIT License | 5 votes |
@Bean("springConnectionFactory") public ConnectionFactory connectionFactory() { CachingConnectionFactory factory = new CachingConnectionFactory(); factory.setUsername(this.user); factory.setPassword(this.pass); factory.setHost(this.host); factory.setPort(this.port); return factory; }
Example #29
Source File: RabbitMQConfiguration.java From Learning-Path-Spring-5-End-to-End-Programming with MIT License | 5 votes |
@Bean("springConnectionFactory") public ConnectionFactory connectionFactory() { CachingConnectionFactory factory = new CachingConnectionFactory(); factory.setUsername(this.user); factory.setPassword(this.pass); factory.setHost(this.host); factory.setPort(this.port); return factory; }
Example #30
Source File: RabbitMQConfig.java From kkbinlog with Apache License 2.0 | 5 votes |
@Bean public ConnectionFactory connectionFactory() { CachingConnectionFactory factory = new CachingConnectionFactory(); factory.setAddresses(host + ":" + port); factory.setVirtualHost(virtualHost); factory.setUsername(username); factory.setPassword(password); return factory; }