JAMES-2545 Introduce RabbitMQ connection factory
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/3b570bc1 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/3b570bc1 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/3b570bc1 Branch: refs/heads/master Commit: 3b570bc1d5c24c872527072d09ab659412ae4b68 Parents: b847583 Author: Antoine Duprat <[email protected]> Authored: Wed Sep 12 14:03:02 2018 +0200 Committer: Benoit Tellier <[email protected]> Committed: Fri Sep 14 10:17:42 2018 +0700 ---------------------------------------------------------------------- .../rabbitmq/RabbitMQConnectionFactory.java | 59 +++++++++++++++++++ .../rabbitmq/RabbitMQConnectionFactoryTest.java | 62 ++++++++++++++++++++ 2 files changed, 121 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/3b570bc1/backends-common/rabbitmq/src/main/java/org/apache/james/backend/rabbitmq/RabbitMQConnectionFactory.java ---------------------------------------------------------------------- diff --git a/backends-common/rabbitmq/src/main/java/org/apache/james/backend/rabbitmq/RabbitMQConnectionFactory.java b/backends-common/rabbitmq/src/main/java/org/apache/james/backend/rabbitmq/RabbitMQConnectionFactory.java new file mode 100644 index 0000000..b6a6ba2 --- /dev/null +++ b/backends-common/rabbitmq/src/main/java/org/apache/james/backend/rabbitmq/RabbitMQConnectionFactory.java @@ -0,0 +1,59 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ +package org.apache.james.backend.rabbitmq; + +import javax.inject.Inject; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.ConnectionFactory; + +public class RabbitMQConnectionFactory { + + private static final Logger LOGGER = LoggerFactory.getLogger(RabbitMQConnectionFactory.class); + + private final ConnectionFactory connectionFactory; + + @Inject + public RabbitMQConnectionFactory(RabbitMQConfiguration rabbitMQConfiguration) { + this.connectionFactory = from(rabbitMQConfiguration); + } + + private ConnectionFactory from(RabbitMQConfiguration rabbitMQConfiguration) { + try { + ConnectionFactory connectionFactory = new ConnectionFactory(); + connectionFactory.setUri(rabbitMQConfiguration.getUri()); + return connectionFactory; + } catch (Exception e) { + LOGGER.error("Fail to create the RabbitMQ connection factory."); + throw new RuntimeException(e); + } + } + + public Connection create() { + try { + return connectionFactory.newConnection(); + } catch (Exception e) { + LOGGER.error("Fail to create a RabbitMQ connection."); + throw new RuntimeException(e); + } + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/3b570bc1/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQConnectionFactoryTest.java ---------------------------------------------------------------------- diff --git a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQConnectionFactoryTest.java b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQConnectionFactoryTest.java new file mode 100644 index 0000000..99cca33 --- /dev/null +++ b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQConnectionFactoryTest.java @@ -0,0 +1,62 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ +package org.apache.james.backend.rabbitmq; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.net.URI; + +import org.junit.jupiter.api.Test; + +class RabbitMQConnectionFactoryTest { + + @Test + void creatingAFactoryShouldWorkWhenConfigurationIsValid() { + RabbitMQConfiguration rabbitMQConfiguration = RabbitMQConfiguration.builder() + .amqpUri(URI.create("amqp://james:james@rabbitmq_host:5672")) + .managementUri(URI.create("http://james:james@rabbitmq_host:15672/api/")) + .build(); + + new RabbitMQConnectionFactory(rabbitMQConfiguration); + } + + @Test + void creatingAFactoryShouldThrowWhenConfigurationIsInvalid() { + RabbitMQConfiguration rabbitMQConfiguration = RabbitMQConfiguration.builder() + .amqpUri(URI.create("badprotocol://james:james@rabbitmq_host:5672")) + .managementUri(URI.create("http://james:james@rabbitmq_host:15672/api/")) + .build(); + + assertThatThrownBy(() -> new RabbitMQConnectionFactory(rabbitMQConfiguration)) + .isInstanceOf(RuntimeException.class); + } + + @Test + void createShouldFailWhenConnectionCantBeDone() { + RabbitMQConfiguration rabbitMQConfiguration = RabbitMQConfiguration.builder() + .amqpUri(URI.create("amqp://james:james@rabbitmq_host:5672")) + .managementUri(URI.create("http://james:james@rabbitmq_host:15672/api/")) + .build(); + + RabbitMQConnectionFactory rabbitMQConnectionFactory = new RabbitMQConnectionFactory(rabbitMQConfiguration); + + assertThatThrownBy(() -> rabbitMQConnectionFactory.create()) + .isInstanceOf(RuntimeException.class); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
