summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGurjeet Bedi <gurjeetb@amdocs.com>2018-06-05 13:13:54 -0400
committerGurjeet Bedi <gurjeetb@amdocs.com>2018-06-07 10:02:28 -0400
commita19b69d1809955293390c8abd868d0eaff318e5b (patch)
tree78376699ef24570dc8413075ecf5842127e55c2a
parent1101277e3bf85ffae75b5a44c9149dd95b30c9b6 (diff)
Event Client library support for RabbitMQ
Update version in RabbitMQ Event Client lib License header update Issue-ID: AAI-1205 Change-Id: I838c184a0b81d89d5d276fbeee373cf5c390fb22 Signed-off-by: Gurjeet Bedi <gurjeetb@amdocs.com>
-rw-r--r--event-client-rabbitmq/pom.xml61
-rw-r--r--event-client-rabbitmq/src/main/java/org/onap/aai/event/client/RabbitMqEventConsumer.java312
-rw-r--r--event-client-rabbitmq/src/main/java/org/onap/aai/event/client/RabbitMqEventPublisher.java281
-rw-r--r--event-client-rabbitmq/src/test/java/org/onap/aai/event/client/TestRabbitMqConsumer.java151
-rw-r--r--event-client-rabbitmq/src/test/java/org/onap/aai/event/client/TestRabbitMqPublisher.java123
-rw-r--r--pom.xml3
6 files changed, 930 insertions, 1 deletions
diff --git a/event-client-rabbitmq/pom.xml b/event-client-rabbitmq/pom.xml
new file mode 100644
index 0000000..6974c68
--- /dev/null
+++ b/event-client-rabbitmq/pom.xml
@@ -0,0 +1,61 @@
+<!--
+
+ ============LICENSE_START=======================================================
+ org.onap.aai
+ ================================================================================
+ Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ Copyright © 2017-2018 European Software Marketing Ltd.
+ ================================================================================
+ Licensed 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.
+ ============LICENSE_END=========================================================
+
+-->
+<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.onap.aai.event-client</groupId>
+ <artifactId>event-client</artifactId>
+ <version>1.3.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>event-client-rabbitmq</artifactId>
+
+ <properties>
+ <common.logging.version>1.2.2</common.logging.version>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>com.rabbitmq</groupId>
+ <artifactId>amqp-client</artifactId>
+ <version>5.1.2</version>
+ </dependency>
+ <dependency>
+ <groupId>org.onap.aai.logging-service</groupId>
+ <artifactId>common-logging</artifactId>
+ <version>${common.logging.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.onap.aai.event-client</groupId>
+ <artifactId>event-client-api</artifactId>
+ <version>${project.parent.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
+ <version>1.10.19</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project>
diff --git a/event-client-rabbitmq/src/main/java/org/onap/aai/event/client/RabbitMqEventConsumer.java b/event-client-rabbitmq/src/main/java/org/onap/aai/event/client/RabbitMqEventConsumer.java
new file mode 100644
index 0000000..c564198
--- /dev/null
+++ b/event-client-rabbitmq/src/main/java/org/onap/aai/event/client/RabbitMqEventConsumer.java
@@ -0,0 +1,312 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2018 European Software Marketing Ltd.
+ * ================================================================================
+ * Licensed 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.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.aai.event.client;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.TimeUnit;
+
+import org.onap.aai.cl.api.Logger;
+import org.onap.aai.cl.eelf.LoggerFactory;
+import org.onap.aai.event.api.EventConsumer;
+import org.onap.aai.event.api.MessageWithOffset;
+
+import com.rabbitmq.client.Address;
+import com.rabbitmq.client.BuiltinExchangeType;
+import com.rabbitmq.client.Channel;
+import com.rabbitmq.client.Connection;
+import com.rabbitmq.client.ConnectionFactory;
+import com.rabbitmq.client.DefaultConsumer;
+import com.rabbitmq.client.Envelope;
+import com.rabbitmq.client.AMQP;
+import com.rabbitmq.client.AMQP.Exchange.DeclareOk;
+
+/**
+ * Event bus client consumer wrapper for RabbitMQ.
+ * This will open a async consumer which will put messages on internal queue so that calling client can then
+ * consume and ackowledge the same based on offset/deliveryTag.
+ * Topic exchange type is powerful and can behave like other exchanges.
+ * When a queue is bound with "#" (hash) binding key - it will receive all the messages, regardless of the routing key - like in fanout exchange.
+ * When special characters "*" (star) and "#" (hash) aren't used in bindings, the topic exchange will behave just like a direct one
+ * @author GURJEETB
+ *
+ */
+public class RabbitMqEventConsumer implements EventConsumer {
+ private static Logger log = LoggerFactory.getInstance().getLogger(RabbitMqEventConsumer.class);
+ private static ConnectionFactory factory = new ConnectionFactory();
+ private BlockingQueue<MessageWithOffset> messageQueue;
+
+ private static String BINDING_CONSUME_ALL = "#";
+
+ private final Connection connection;
+ private final Channel channel;
+ private final String queueName;
+ private DeclareOk exchangeInfo;
+ private Long lastDeliveryTag;
+ private long timeout = 5000;
+ private com.rabbitmq.client.AMQP.Queue.DeclareOk queueInfo;
+
+ /**
+ * (intended for testing prupose only)
+ * @param connFactory
+ */
+ static void setConnectionFactory(ConnectionFactory connFactory) {
+ factory = connFactory;
+ }
+
+ /**
+ * (intended for testing prupose only)
+ * @param messageQueue
+ */
+ public void setMessageQueue(BlockingQueue<MessageWithOffset> messageQueue) {
+ this.messageQueue = messageQueue;
+ }
+
+ /**
+ * Constructor to open a consumer on single host port for a topic exchange with specific queue name which will
+ * consume all messages from topic
+ * @param host
+ * @param port
+ * @param userName
+ * @param password
+ * @param exchangeName
+ * @param queueName
+ * @throws Exception
+ */
+ public RabbitMqEventConsumer(String host, int port, String userName, String password, String exchangeName, String queueName) throws Exception {
+ this(host, port, userName, password, BuiltinExchangeType.TOPIC.name(), exchangeName, queueName, BINDING_CONSUME_ALL);
+ }
+
+ /**
+ * Constructor to open a consumer on single host port for a exchange with specific queue name which will
+ * consume all messages from topic
+ * @param host
+ * @param port
+ * @param userName
+ * @param password
+ * @param exchangeType - Supported values - DIRECT, FANOUT, TOPIC, HEADERS
+ * @param exchangeName
+ * @param queueName
+ * @throws Exception
+ */
+ public RabbitMqEventConsumer(String host, int port, String userName, String password, String exchangeType, String exchangeName, String queueName) throws Exception {
+ this(host, port, userName, password, exchangeType, exchangeName, queueName, BINDING_CONSUME_ALL);
+ }
+
+ /**
+ * Constructor to open a consumer on single host port for a exchange with specific queue name
+ * @param host
+ * @param port
+ * @param userName
+ * @param password
+ * @param exchangeType
+ * @param exchangeName
+ * @param queueName
+ * @param bindingKey - Bind the queue to specific messages only
+ * @throws Exception
+ */
+ public RabbitMqEventConsumer(String host, int port, String userName, String password, String exchangeType, String exchangeName, String queueName, String bindingKey) throws Exception {
+ this(buildMap(host, port), userName, password, BuiltinExchangeType.valueOf(exchangeType), exchangeName, queueName, bindingKey, new HashMap<String, Object>());
+ }
+
+ /**
+ * Constructor to open a consumer on multiple host port for a topic exchange with specific queue name which will
+ * consume all messages from topic
+ * @param hostPortMap
+ * @param userName
+ * @param password
+ * @param exchangeName
+ * @param queueName
+ * @throws Exception
+ */
+ public RabbitMqEventConsumer(Map<String, Integer> hostPortMap, String userName, String password, String exchangeName, String queueName) throws Exception {
+ this(hostPortMap, userName, password, BuiltinExchangeType.TOPIC.name(), exchangeName, queueName, BINDING_CONSUME_ALL);
+ }
+
+ /**
+ * Constructor to open a consumer on multiple host port for a exchange with specific queue name which will
+ * consume all messages from topic
+ * @param hostPortMap
+ * @param userName
+ * @param password
+ * @param exchangeType - Supported values - DIRECT, FANOUT, TOPIC, HEADERS
+ * @param exchangeName
+ * @param queueName
+ * @throws Exception
+ */
+ public RabbitMqEventConsumer(Map<String, Integer> hostPortMap, String userName, String password, String exchangeType, String exchangeName, String queueName) throws Exception {
+ this(hostPortMap, userName, password, exchangeType, exchangeName, queueName, BINDING_CONSUME_ALL);
+ }
+
+ /**
+ * Constructor to open a consumer on multiple host port for a exchange with specific queue name
+ * @param hostPortMap
+ * @param userName
+ * @param password
+ * @param exchangeType
+ * @param exchangeName
+ * @param queueName
+ * @param bindingKey - Bind the queue to specific messages only
+ * @throws Exception
+ */
+ public RabbitMqEventConsumer(Map<String, Integer> hostPortMap, String userName, String password, String exchangeType, String exchangeName, String queueName, String bindingKey) throws Exception {
+ this(hostPortMap, userName, password, BuiltinExchangeType.valueOf(exchangeType), exchangeName, queueName, bindingKey, new HashMap<String, Object>());
+ }
+
+ public RabbitMqEventConsumer(Map<String, Integer> hostPortMap, String userName, String password, BuiltinExchangeType exchangeType, String exchangeName, String queueName, String bindingKey, Map<String, Object> exchangeArguments) throws Exception {
+ messageQueue = new ArrayBlockingQueue<>(1000);
+ List<Address> addresses = new ArrayList<Address>();
+ Iterator<String> iter = hostPortMap.keySet().iterator();
+ while (iter.hasNext())
+ {
+ String host = iter.next();
+ int port = hostPortMap.get(host);
+ Address add = new Address(host,port);
+ addresses.add(add);
+ }
+ factory.setUsername(userName);
+ factory.setPassword(password);
+ connection = factory.newConnection(addresses);
+ channel = connection.createChannel();
+ exchangeInfo = channel.exchangeDeclare(exchangeName, exchangeType, true, false, exchangeArguments);
+ this.queueName = queueName;
+ channel.queueDeclare(queueName, true, false, false, null);
+ channel.queueBind(queueName, exchangeName, bindingKey);
+ String consumerTag = channel.basicConsume(queueName, false, new CallBackConsumer(channel)); //AutoAck is false
+ }
+
+ public RabbitMqEventConsumer(String host, int port, String userName, String password, String queue) throws Exception {
+ this(buildMap(host, port), userName, password, queue, new HashMap<String, Object>());
+ }
+
+ public RabbitMqEventConsumer(Map<String, Integer> hostPortMap, String userName, String password, String queue, Map<String, Object> queueArguments) throws Exception {
+ messageQueue = new ArrayBlockingQueue<>(1000);
+ List<Address> addresses = new ArrayList<Address>();
+ Iterator<String> iter = hostPortMap.keySet().iterator();
+ while (iter.hasNext())
+ {
+ String host = iter.next();
+ int port = hostPortMap.get(host);
+ Address add = new Address(host,port);
+ addresses.add(add);
+ }
+ factory.setUsername(userName);
+ factory.setPassword(password);
+ connection = factory.newConnection(addresses);
+ channel = connection.createChannel();
+ this.queueName = queue;
+ queueInfo = channel.queueDeclare(queueName, true, false, false, queueArguments);
+ String consumerTag = channel.basicConsume(queueName, false, new CallBackConsumer(channel)); //AutoAck is false
+ }
+
+ private static Map<String, Integer> buildMap(String host, Integer port) {
+ Map<String, Integer> hostPortMap = new HashMap<String, Integer>();
+ hostPortMap.put(host, port);
+ return hostPortMap;
+ }
+
+ @Override
+ public Iterable<String> consumeAndCommit() throws Exception {
+ Iterable<String> list = consume();
+ commitOffsets();
+ return list;
+ }
+
+ @Override
+ public Iterable<String> consume() throws Exception {
+ List<String> list = new ArrayList<>();
+ MessageWithOffset record = null;
+ if(messageQueue.peek()!=null) {
+ do
+ {
+ record = messageQueue.poll(1000, TimeUnit.MILLISECONDS);
+ lastDeliveryTag = record.getOffset();
+ list.add(record.getMessage());
+ }while(messageQueue.peek()!=null);
+ }
+ return list;
+ }
+
+ @Override
+ public Iterable<MessageWithOffset> consumeWithOffsets() throws Exception {
+ List<MessageWithOffset> list = new ArrayList<>();
+ MessageWithOffset record = null;
+ if(messageQueue.peek()!=null) {
+ do
+ {
+ record = messageQueue.poll(1000, TimeUnit.MILLISECONDS);
+ lastDeliveryTag = record.getOffset();
+ list.add(record);
+ }while(messageQueue.peek()!=null);
+ }
+ return list;
+ }
+
+ @Override
+ public void commitOffsets() throws Exception {
+ if(lastDeliveryTag != null)
+ {
+ channel.basicAck(lastDeliveryTag, true); //Ack messages upto lastDeliveryTag or offset so that they can be marked
+ lastDeliveryTag = null;
+ }
+ }
+
+ @Override
+ public void commitOffsets(long offset) throws Exception {
+ channel.basicAck(offset, true); //Ack messages upto lastDeliveryTag or offset so that they can be marked
+ }
+
+ /**
+ * Closes the channel
+ * @throws Exception
+ */
+ public void close() throws Exception {
+ channel.close();
+ connection.close();
+ }
+
+ class CallBackConsumer extends DefaultConsumer{
+ CallBackConsumer(Channel channel) {
+ super(channel);
+ }
+ @Override
+ public void handleDelivery(String consumerTag, Envelope envelope,
+ AMQP.BasicProperties properties, byte[] body) throws IOException {
+ String message = new String(body, "UTF-8");
+ try
+ {
+ MessageWithOffset record = new MessageWithOffset(envelope.getDeliveryTag(), message);
+ messageQueue.offer(record, timeout, TimeUnit.MILLISECONDS);
+ }
+ catch(Exception e)
+ {
+ log.debug(" Got exception while handling message="+e.getMessage()+" Will be reposting to queue");
+ channel.basicNack(envelope.getDeliveryTag(), false, true); //Explicit Ack with requeue
+ }
+ }
+ }
+}
diff --git a/event-client-rabbitmq/src/main/java/org/onap/aai/event/client/RabbitMqEventPublisher.java b/event-client-rabbitmq/src/main/java/org/onap/aai/event/client/RabbitMqEventPublisher.java
new file mode 100644
index 0000000..a7e0ee3
--- /dev/null
+++ b/event-client-rabbitmq/src/main/java/org/onap/aai/event/client/RabbitMqEventPublisher.java
@@ -0,0 +1,281 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2018 European Software Marketing Ltd.
+ * ================================================================================
+ * Licensed 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.aai.event.client;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.onap.aai.cl.api.Logger;
+import org.onap.aai.cl.eelf.LoggerFactory;
+import org.onap.aai.event.api.EventPublisher;
+
+import com.rabbitmq.client.AMQP.Exchange.DeclareOk;
+import com.rabbitmq.client.Address;
+import com.rabbitmq.client.BuiltinExchangeType;
+import com.rabbitmq.client.Channel;
+import com.rabbitmq.client.Connection;
+import com.rabbitmq.client.ConnectionFactory;
+
+/**
+ * Event bus client publisher wrapper for RabbitMQ
+ * @author GURJEETB
+ *
+ */
+
+public class RabbitMqEventPublisher implements EventPublisher {
+ private static Logger log = LoggerFactory.getInstance().getLogger(RabbitMqEventPublisher.class);
+ private static ConnectionFactory factory = new ConnectionFactory();
+
+ private final Connection connection;
+ private final Channel channel;
+ private final String exchangeName;
+ private final String queueName;
+ private final String routingKey;
+ private DeclareOk exchangeInfo;
+ private com.rabbitmq.client.AMQP.Queue.DeclareOk queueInfo;
+
+ static void setConnectionFactory(ConnectionFactory connFactory) {
+ factory = connFactory;
+ }
+ /**
+ * Constructor for single host port for a topic exchange
+ * @param host
+ * @param port
+ * @param userName
+ * @param password
+ * @param exchangeName
+ * @throws Exception
+ */
+ public RabbitMqEventPublisher(String host, int port, String userName, String password, String exchangeName) throws Exception {
+ this(host, port, userName, password, BuiltinExchangeType.TOPIC.name(), exchangeName,"");
+ }
+
+ /**
+ * Constructor for single host port for a specific exchange type
+ * @param host
+ * @param port
+ * @param userName
+ * @param password
+ * @param exchangeType - Supported values - DIRECT, FANOUT, TOPIC, HEADERS
+ * @param exchangeName
+ * @throws Exception
+ */
+ public RabbitMqEventPublisher(String host, int port, String userName, String password, String exchangeType, String exchangeName) throws Exception {
+ this(host,port, userName, password, exchangeType, exchangeName,"");
+ }
+
+ /**
+ * Constructor for single host port for a specific exchange type and specific routing key
+ * @param host
+ * @param port
+ * @param userName
+ * @param password
+ * @param exchangeType
+ * @param exchangeName
+ * @param routingKey
+ * @throws Exception
+ */
+ public RabbitMqEventPublisher(String host, int port, String userName, String password, String exchangeType, String exchangeName, String routingKey) throws Exception {
+ this(buildMap(host,port), userName, password, BuiltinExchangeType.valueOf(exchangeType), exchangeName,routingKey, new HashMap<String, Object>());
+ }
+
+ /**
+ * Constructor for multiple host and port for a topic exchange
+ * @param hostPortMap
+ * @param userName
+ * @param password
+ * @param exchangeName
+ * @throws Exception
+ */
+ public RabbitMqEventPublisher(Map<String, Integer> hostPortMap, String userName, String password, String exchangeName) throws Exception {
+ this(hostPortMap, userName, password, BuiltinExchangeType.TOPIC.name(), exchangeName,"");
+ }
+
+ /**
+ * Constructor for multiple host port for a specific exchange type
+ * @param hostPortMap
+ * @param userName
+ * @param password
+ * @param exchangeType - Supported values - DIRECT, FANOUT, TOPIC, HEADERS
+ * @param exchangeName
+ * @throws Exception
+ */
+ public RabbitMqEventPublisher(Map<String, Integer> hostPortMap, String userName, String password, String exchangeType, String exchangeName) throws Exception {
+ this(hostPortMap, userName, password, exchangeType , exchangeName,"");
+ }
+
+ /**
+ * Constructor for multiple host port for a specific exchange type and specific routing key
+ * @param hostPortMap
+ * @param userName
+ * @param password
+ * @param exchangeType
+ * @param exchangeName
+ * @param routingKey
+ * @throws Exception
+ */
+ public RabbitMqEventPublisher(Map<String, Integer> hostPortMap, String userName, String password, String exchangeType, String exchangeName, String routingKey) throws Exception {
+ this(hostPortMap, userName, password, BuiltinExchangeType.valueOf(exchangeType), exchangeName,routingKey, new HashMap<String, Object>());
+ }
+
+ public RabbitMqEventPublisher(Map<String, Integer> hostPortMap, String userName, String password, BuiltinExchangeType exchangeType, String exchangeName, String routingKey, Map<String, Object> exchangeArguments) throws Exception {
+ List<Address> addresses = new ArrayList<Address>();
+ Iterator<String> iter = hostPortMap.keySet().iterator();
+ while (iter.hasNext())
+ {
+ String host = iter.next();
+ int port = hostPortMap.get(host);
+ Address add = new Address(host,port);
+ addresses.add(add);
+ }
+ factory.setUsername(userName);
+ factory.setPassword(password);
+ connection = factory.newConnection(addresses);
+ channel = connection.createChannel();
+ this.exchangeName = exchangeName;
+ exchangeInfo = channel.exchangeDeclare(exchangeName, exchangeType, true, false, exchangeArguments); //Durable exchange and non delete
+ queueName = null;
+ this.routingKey=routingKey;
+ }
+
+ /**
+ * Constructor for single host port for a queue connection
+ * @param host
+ * @param port
+ * @param userName
+ * @param password
+ * @param queue
+ * @param queueArguments
+ * @throws Exception
+ */
+ public RabbitMqEventPublisher(String host, int port, String userName, String password, String queue, Map<String, Object> queueArguments) throws Exception {
+ this(buildMap(host, port), userName, password, queue, queueArguments);
+ }
+
+ /**
+ * Constructor for multiple host port for a queue connection
+ * @param hostPortMap
+ * @param userName
+ * @param password
+ * @param queue
+ * @param queueArguments
+ * @throws Exception
+ */
+ public RabbitMqEventPublisher(Map<String, Integer> hostPortMap, String userName, String password, String queue, Map<String, Object> queueArguments) throws Exception {
+ List<Address> addresses = new ArrayList<Address>();
+ Iterator<String> iter = hostPortMap.keySet().iterator();
+ while (iter.hasNext())
+ {
+ String host = iter.next();
+ int port = hostPortMap.get(host);
+ Address add = new Address(host,port);
+ addresses.add(add);
+ }
+ factory.setUsername(userName);
+ factory.setPassword(password);
+ connection = factory.newConnection(addresses);
+ channel = connection.createChannel();
+ exchangeName = "";
+ routingKey=queue;
+ this.queueName = queue;
+ queueInfo = channel.queueDeclare(queueName, true, false, false, queueArguments); //Durable, non exclusive and non auto delete queue
+ }
+
+ private static Map<String, Integer> buildMap(String host, Integer port) {
+ Map<String, Integer> hostPortMap = new HashMap<String, Integer>();
+ hostPortMap.put(host, port);
+ return hostPortMap;
+ }
+
+ @Override
+ public void close() throws Exception {
+ channel.close();
+ connection.close();
+ }
+
+ @Override
+ public void sendAsync(String message) throws Exception {
+ sendSync(message);
+ }
+
+ @Override
+ public void sendAsync(Collection<String> messages) throws Exception {
+ sendSync(messages);
+ }
+
+ @Override
+ public void sendAsync(String routingParam, String message) throws Exception {
+ sendSync(routingParam, message);
+ }
+
+ @Override
+ public void sendAsync(String routingParam, Collection<String> messages) throws Exception {
+ sendSync(routingParam, messages);
+ }
+
+ @Override
+ public int sendSync(String message) throws Exception {
+ channel.basicPublish(exchangeName, routingKey, null, message.getBytes());
+ log.debug(" [x] Sent '" + message + "'");
+ return 1;
+ }
+
+ @Override
+ public int sendSync(Collection<String> messages) throws Exception {
+ log.debug("Publishing" + messages.size() + " messages ");
+ for (String message : messages) {
+ sendSync(message);
+ }
+ return messages.size();
+ }
+
+ @Override
+ public int sendSync(String routingParam, String message) throws Exception {
+ //Can only route if exchange is setup
+ if(queueName == null) {
+ channel.basicPublish(exchangeName, routingParam, null, message.getBytes());
+ return 1;
+ }
+ else
+ throw new UnsupportedOperationException("Routing without exchange");
+
+ }
+
+ @Override
+ public int sendSync(String routingParam, Collection<String> messages) throws Exception {
+ log.debug("Publishing" + messages.size() + " messages ");
+ //Can only route if exchange is setup
+ if(queueName == null) {
+ for (String message : messages) {
+ sendSync(routingParam, message);
+ }
+ return messages.size();
+ }
+ else
+ throw new UnsupportedOperationException("Routing without exchange");
+ }
+
+}
diff --git a/event-client-rabbitmq/src/test/java/org/onap/aai/event/client/TestRabbitMqConsumer.java b/event-client-rabbitmq/src/test/java/org/onap/aai/event/client/TestRabbitMqConsumer.java
new file mode 100644
index 0000000..df4c54b
--- /dev/null
+++ b/event-client-rabbitmq/src/test/java/org/onap/aai/event/client/TestRabbitMqConsumer.java
@@ -0,0 +1,151 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2018 European Software Marketing Ltd.
+ * ================================================================================
+ * Licensed 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.aai.event.client;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.onap.aai.event.api.MessageWithOffset;
+
+import com.rabbitmq.client.Address;
+import com.rabbitmq.client.BuiltinExchangeType;
+import com.rabbitmq.client.Channel;
+import com.rabbitmq.client.Connection;
+import com.rabbitmq.client.ConnectionFactory;
+import com.rabbitmq.client.Consumer;
+import com.rabbitmq.client.AMQP.Exchange.DeclareOk;
+
+@RunWith(MockitoJUnitRunner.class)
+public class TestRabbitMqConsumer {
+ @Mock
+ public ConnectionFactory mockConnectionFactory;
+
+ @Mock
+ public Connection mockConnection;
+
+ @Mock
+ public Channel mockChannel;
+
+ @Mock
+ public DeclareOk mockDeclareOK;
+
+ @Mock
+ BlockingQueue<MessageWithOffset> mqueue;
+
+ BuiltinExchangeType topicEx = BuiltinExchangeType.TOPIC;
+ TimeUnit unit = TimeUnit.MILLISECONDS;
+ Map<String, Integer> hostPortMap = new HashMap<String, Integer>();
+
+ @Before
+ public void init() throws Exception {
+ RabbitMqEventConsumer.setConnectionFactory(mockConnectionFactory);
+ }
+
+ @Test
+ public void testConstructor() throws Exception {
+ Mockito.when(mockConnectionFactory.newConnection(Mockito.anyListOf(Address.class))).thenReturn(mockConnection);
+ Mockito.when(mockConnection.createChannel()).thenReturn(mockChannel);
+ Mockito.when(mockChannel.exchangeDeclare(Mockito.any(), Mockito.eq(topicEx), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyMap())).thenReturn(mockDeclareOK);
+ Mockito.when(mockChannel.basicConsume(Mockito.any(), Mockito.anyBoolean(), Mockito.any(Consumer.class))).thenReturn(Mockito.anyString());
+ new RabbitMqEventConsumer("hosts", 0, "userName", "password", "exchangeName", "queueName");
+ new RabbitMqEventConsumer("hosts", 0, "userName", "password", BuiltinExchangeType.DIRECT.name(), "exchangeName", "queueName");
+ new RabbitMqEventConsumer(hostPortMap, "userName", "password", "exchangeName", "queueName");
+ new RabbitMqEventConsumer(hostPortMap, "userName", "password", BuiltinExchangeType.DIRECT.name(), "exchangeName", "queueName");
+ new RabbitMqEventConsumer("hosts", 0, "userName", "password", "queue");
+ }
+
+ @Test
+ public void consumeZeroRecords() throws Exception {
+ Mockito.when(mockConnectionFactory.newConnection(Mockito.anyListOf(Address.class))).thenReturn(mockConnection);
+ Mockito.when(mockConnection.createChannel()).thenReturn(mockChannel);
+ Mockito.when(mockChannel.exchangeDeclare(Mockito.any(), Mockito.eq(topicEx), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyMap())).thenReturn(mockDeclareOK);
+ Mockito.when(mockChannel.basicConsume(Mockito.any(), Mockito.anyBoolean(), Mockito.any(Consumer.class))).thenReturn(Mockito.anyString());
+ RabbitMqEventConsumer consumer = new RabbitMqEventConsumer("hosts", 0, "userName", "password", "exchangeName", "queueName");
+ consumer.consume();
+ consumer.consumeWithOffsets();
+ consumer.consumeAndCommit();
+ consumer.close();
+ }
+
+ @Test
+ public void commitOffsets() throws Exception {
+ Mockito.when(mockConnectionFactory.newConnection(Mockito.anyListOf(Address.class))).thenReturn(mockConnection);
+ Mockito.when(mockConnection.createChannel()).thenReturn(mockChannel);
+ Mockito.when(mockChannel.exchangeDeclare(Mockito.any(), Mockito.eq(topicEx), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyMap())).thenReturn(mockDeclareOK);
+ Mockito.when(mockChannel.basicConsume(Mockito.any(), Mockito.anyBoolean(), Mockito.any(Consumer.class))).thenReturn(Mockito.anyString());
+ RabbitMqEventConsumer consumer = new RabbitMqEventConsumer("hosts", 0, "userName", "password", "exchangeName", "queueName");
+ consumer.commitOffsets();
+ consumer.commitOffsets(0L);
+ consumer.close();
+ }
+
+ @Test
+ public void consumeMultipleRecords() throws Exception {
+ Mockito.when(mockConnectionFactory.newConnection(Mockito.anyListOf(Address.class))).thenReturn(mockConnection);
+ Mockito.when(mockConnection.createChannel()).thenReturn(mockChannel);
+ Mockito.when(mockChannel.exchangeDeclare(Mockito.any(), Mockito.eq(topicEx), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyMap())).thenReturn(mockDeclareOK);
+ Mockito.when(mockChannel.basicConsume(Mockito.any(), Mockito.anyBoolean(), Mockito.any(Consumer.class))).thenReturn(Mockito.anyString());
+ List<MessageWithOffset> records = buildTestMessages(2);
+ mqueue = new ArrayBlockingQueue<>(2);
+ mqueue.addAll(records);
+ RabbitMqEventConsumer consumer = new RabbitMqEventConsumer("hosts", 0, "userName", "password", "exchangeName", "queueName");
+ consumer.setMessageQueue(mqueue);
+ consumer.consumeAndCommit();
+ consumer.close();
+ }
+
+ @Test
+ public void consumeWithOffSetsMultipleRecords() throws Exception {
+ Mockito.when(mockConnectionFactory.newConnection(Mockito.anyListOf(Address.class))).thenReturn(mockConnection);
+ Mockito.when(mockConnection.createChannel()).thenReturn(mockChannel);
+ Mockito.when(mockChannel.exchangeDeclare(Mockito.any(), Mockito.eq(topicEx), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyMap())).thenReturn(mockDeclareOK);
+ Mockito.when(mockChannel.basicConsume(Mockito.any(), Mockito.anyBoolean(), Mockito.any(Consumer.class))).thenReturn(Mockito.anyString());
+ List<MessageWithOffset> records = buildTestMessages(2);
+ mqueue = new ArrayBlockingQueue<>(2);
+ mqueue.addAll(records);
+ RabbitMqEventConsumer consumer = new RabbitMqEventConsumer("hosts", 0, "userName", "password", "exchangeName", "queueName");
+ consumer.setMessageQueue(mqueue);
+ consumer.consumeWithOffsets();
+ consumer.commitOffsets();
+ consumer.close();
+ }
+
+ private List<MessageWithOffset> buildTestMessages(int nbrOfMessages) {
+ List<MessageWithOffset> msgList = new ArrayList<MessageWithOffset>();
+ for(int i=0;i<nbrOfMessages;i++) {
+ MessageWithOffset message = new MessageWithOffset(i,"Message:"+i);
+ msgList.add(message);
+ }
+ return msgList;
+ }
+}
diff --git a/event-client-rabbitmq/src/test/java/org/onap/aai/event/client/TestRabbitMqPublisher.java b/event-client-rabbitmq/src/test/java/org/onap/aai/event/client/TestRabbitMqPublisher.java
new file mode 100644
index 0000000..e41126f
--- /dev/null
+++ b/event-client-rabbitmq/src/test/java/org/onap/aai/event/client/TestRabbitMqPublisher.java
@@ -0,0 +1,123 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2018 European Software Marketing Ltd.
+ * ================================================================================
+ * Licensed 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.aai.event.client;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import com.rabbitmq.client.AMQP.Exchange.DeclareOk;
+import com.rabbitmq.client.Address;
+import com.rabbitmq.client.BuiltinExchangeType;
+import com.rabbitmq.client.Channel;
+import com.rabbitmq.client.Connection;
+import com.rabbitmq.client.ConnectionFactory;
+
+@RunWith(MockitoJUnitRunner.class)
+public class TestRabbitMqPublisher {
+ @Mock
+ public ConnectionFactory mockConnectionFactory;
+
+ @Mock
+ public Connection mockConnection;
+
+ @Mock
+ public Channel mockChannel;
+
+ @Mock
+ public DeclareOk mockDeclareOK;
+
+ BuiltinExchangeType topicEx = BuiltinExchangeType.TOPIC;
+ Map<String, Integer> hostPortMap = new HashMap<String, Integer>();
+
+ @Before
+ public void init() throws Exception {
+ RabbitMqEventPublisher.setConnectionFactory(mockConnectionFactory);
+ }
+
+ @Test
+ public void testConstructors() throws Exception {
+ Mockito.when(mockConnectionFactory.newConnection(Mockito.anyListOf(Address.class))).thenReturn(mockConnection);
+ Mockito.when(mockConnection.createChannel()).thenReturn(mockChannel);
+ Mockito.when(mockChannel.exchangeDeclare(Mockito.any(), Mockito.eq(topicEx), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyMap())).thenReturn(mockDeclareOK);
+ new RabbitMqEventPublisher("hosts", 0, "userName", "password", "exchangeName");
+ new RabbitMqEventPublisher("hosts", 0, "userName", "password", BuiltinExchangeType.DIRECT.name(), "exchangeName");
+ new RabbitMqEventPublisher(hostPortMap, "userName", "password", "exchangeName");
+ new RabbitMqEventPublisher(hostPortMap, "userName", "password", BuiltinExchangeType.DIRECT.name(), "exchangeName");
+ new RabbitMqEventPublisher("hosts", 0, "userName", "password", "queueName",new HashMap<String, Object>());
+ }
+
+
+ @Test
+ public void publishSynchronous() throws Exception {
+ Mockito.when(mockConnectionFactory.newConnection(Mockito.anyListOf(Address.class))).thenReturn(mockConnection);
+ Mockito.when(mockConnection.createChannel()).thenReturn(mockChannel);
+ Mockito.when(mockChannel.exchangeDeclare(Mockito.any(), Mockito.eq(topicEx), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyMap())).thenReturn(mockDeclareOK);
+ RabbitMqEventPublisher publisher = new RabbitMqEventPublisher("hosts", 0, "userName", "password", "exchangeName");
+ publisher.sendSync("");
+ publisher.sendSync(Arrays.asList(""));
+ publisher.sendSync("key", "");
+ publisher.sendSync("key", Arrays.asList(""));
+ publisher.close();
+ }
+
+ @Test
+ public void publishAsynchronous() throws Exception {
+ Mockito.when(mockConnectionFactory.newConnection(Mockito.anyListOf(Address.class))).thenReturn(mockConnection);
+ Mockito.when(mockConnection.createChannel()).thenReturn(mockChannel);
+ Mockito.when(mockChannel.exchangeDeclare(Mockito.any(), Mockito.eq(topicEx), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyMap())).thenReturn(mockDeclareOK);
+ RabbitMqEventPublisher publisher = new RabbitMqEventPublisher("hosts", 0, "userName", "password", "exchangeName");
+ publisher.sendAsync("");
+ publisher.sendAsync(Arrays.asList(""));
+ publisher.sendAsync("key", "");
+ publisher.sendAsync("key", Arrays.asList(""));
+ publisher.close();
+ }
+ @Test(expected = UnsupportedOperationException.class)
+ public void UnsupportedMessageTest() throws Exception {
+ Mockito.when(mockConnectionFactory.newConnection(Mockito.anyListOf(Address.class))).thenReturn(mockConnection);
+ Mockito.when(mockConnection.createChannel()).thenReturn(mockChannel);
+ Mockito.when(mockChannel.exchangeDeclare(Mockito.any(), Mockito.eq(topicEx), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyMap())).thenReturn(mockDeclareOK);
+ RabbitMqEventPublisher publisher = new RabbitMqEventPublisher("hosts", 0, "userName", "password", "queueName",new HashMap<String, Object>());
+ publisher.sendSync("key", "");
+ publisher.sendSync("key", Arrays.asList(""));
+ publisher.close();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void UnSupportedMultiMessageTests() throws Exception {
+ Mockito.when(mockConnectionFactory.newConnection(Mockito.anyListOf(Address.class))).thenReturn(mockConnection);
+ Mockito.when(mockConnection.createChannel()).thenReturn(mockChannel);
+ Mockito.when(mockChannel.exchangeDeclare(Mockito.any(), Mockito.eq(topicEx), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyMap())).thenReturn(mockDeclareOK);
+ RabbitMqEventPublisher publisher = new RabbitMqEventPublisher("hosts", 0, "userName", "password", "queueName",new HashMap<String, Object>());
+ publisher.sendSync("key", Arrays.asList(""));
+ publisher.close();
+ }
+
+}
diff --git a/pom.xml b/pom.xml
index 5b5b6e1..fdf9f9c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -32,7 +32,7 @@
<groupId>org.onap.aai.event-client</groupId>
<artifactId>event-client</artifactId>
- <version>1.2.3-SNAPSHOT</version>
+ <version>1.3.0-SNAPSHOT</version>
<name>aai-event-client</name>
<packaging>pom</packaging>
@@ -47,6 +47,7 @@
<module>event-client-api</module>
<module>event-client-dmaap</module>
<module>event-client-kafka</module>
+ <module>event-client-rabbitmq</module>
</modules>
<dependencies>