Java tutorial
/******************************************************************************* * Copyright 2016 Hewlett Packard Enterprise Development LP * * 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. *******************************************************************************/ package com.hpe.software.amqp; import java.io.IOException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.hpe.software.exception.InvalidOperationException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.ConsumerCancelledException; import com.rabbitmq.client.QueueingConsumer; import com.rabbitmq.client.ShutdownSignalException; /** * @author straif * * Connector for RabbitMQ */ public class RabbitMQConnector implements IAmqpConnector { private static final Logger log = LoggerFactory.getLogger(RabbitMQConnector.class); /** * RabbitMQ connection factory instance */ protected ConnectionFactory factory = new ConnectionFactory(); /** * Channel to RabbitMQ server */ protected Channel channel; /** * Consumer encapsulation */ protected QueueingConsumer consumer; /** * The declared exchange */ protected String exchangeName; /** * The declared queue name */ protected String queueName; /** * Routing key for queue */ protected String routingKey; /** * Last Delivery */ QueueingConsumer.Delivery delivery; /* * (non-Javadoc) * @see com.agmgithub.amqp.IAmqpConnector#connect(java.lang.String, java.lang.String, java.lang.String) */ @Override public void connect(String server, String user, String password) throws IOException { factory.setHost(server); factory.setUsername(user); factory.setPassword(password); Connection connection = factory.newConnection(); if (channel != null) { try { channel.abort(); } catch (Exception ex) { log.debug(ex.toString()); } channel = null; } channel = connection.createChannel(); //init to null exchangeName = null; queueName = null; routingKey = null; consumer = null; delivery = null; log.info("Connected to " + server); } /* * (non-Javadoc) * @see com.agmgithub.amqp.IAmqpConnector#connectToExchange(java.lang.String, java.lang.String, boolean) */ @Override public void connectToExchange(String exchangeName, String exchangeType, boolean durable) throws InvalidOperationException, IOException { if (channel == null) { throw new InvalidOperationException("Need to connect before connecting to an exchange"); } channel.exchangeDeclare(exchangeName, exchangeType, durable); this.exchangeName = exchangeName; log.info("Declared exchange " + exchangeName); } /* * (non-Javadoc) * @see com.agmgithub.amqp.IAmqpConnector#publishToExchange(java.lang.String, java.lang.String, byte[]) */ @Override public void publishToExchange(String exchangeName, String routingKey, byte[] message) throws InvalidOperationException, IOException { if (channel == null || exchangeName == null) { throw new InvalidOperationException( "Need to connect and connectToExchange before pulish a message to an exchange"); } channel.basicPublish(exchangeName, routingKey, null, message); log.info("Published message to exchange " + exchangeName); } /* * (non-Javadoc) * @see com.hpe.agmgithub.amqp.IAmqpConnector#connectQueue(java.lang.String, java.lang.String, boolean, boolean, boolean) */ @Override public String connectQueue(String queueName, String routingKey, boolean durable, boolean exclusive, boolean autoDelete) throws InvalidOperationException, IOException { if (channel == null || exchangeName == null) { throw new InvalidOperationException("Need to connect and connectToExchange before connecting a queue"); } //declare queue and bind to the exchange this.queueName = channel.queueDeclare(queueName, durable, exclusive, autoDelete, null).getQueue(); channel.queueBind(queueName, exchangeName, routingKey); this.routingKey = routingKey; //start the consumer this.consumer = new QueueingConsumer(channel); return channel.basicConsume(queueName, false, consumer); } /* * (non-Javadoc) * @see com.hpe.agmgithub.amqp.IAmqpConnector#nextDelivery() */ @Override public long nextDelivery() throws InvalidOperationException, ShutdownSignalException, ConsumerCancelledException, InterruptedException { if (channel == null || exchangeName == null || consumer == null) { throw new InvalidOperationException( "Need to connect, connectToExchange and connectQueue before waiting for a delivery."); } delivery = consumer.nextDelivery(); return delivery.getEnvelope().getDeliveryTag(); } /* * (non-Javadoc) * @see com.hpe.agmgithub.amqp.IAmqpConnector#nextDelivery() */ @Override public long nextDelivery(long timeout) throws InvalidOperationException, ShutdownSignalException, ConsumerCancelledException, InterruptedException { long deliveryTag = -1; if (channel == null || exchangeName == null || consumer == null) { throw new InvalidOperationException( "Need to connect, connectToExchange and connectQueue before waiting for a delivery."); } delivery = consumer.nextDelivery(timeout); if (delivery != null) { deliveryTag = delivery.getEnvelope().getDeliveryTag(); } return deliveryTag; } /* * (non-Javadoc) * @see com.hpe.agmgithub.amqp.IAmqpConnector#getDeliveryBody(long) */ @Override public byte[] getDeliveryBody(long deliveryTag) throws InvalidOperationException { if (delivery == null) { throw new InvalidOperationException("Need to first call nextDelivery()"); } if (delivery.getEnvelope().getDeliveryTag() != deliveryTag) { throw new InvalidOperationException("Invalid deliver tag."); } return delivery.getBody(); } /* * (non-Javadoc) * @see com.hpe.agmgithub.amqp.IAmqpConnector#basicNack(long, boolean, boolean) */ @Override public void basicNack(long deliveryTag, boolean multiple, boolean requeue) throws InvalidOperationException, IOException { if (channel == null) { throw new InvalidOperationException("Need to connect before Nacknowledge a delivery."); } channel.basicNack(deliveryTag, multiple, requeue); } /** * Cleaning up */ public void finalize() { if (channel != null) { try { channel.abort(); } catch (Exception ex) { } channel = null; } } /* * (non-Javadoc) * @see com.hpe.agmgithub.amqp.IAmqpConnector#basicAck(long, boolean) */ @Override public void basicAck(long deliveryTag, boolean multiple) throws InvalidOperationException, IOException { if (channel == null) { throw new InvalidOperationException("Need to connect before acknowledge a delivery."); } channel.basicAck(deliveryTag, multiple); } /* * (non-Javadoc) * @see com.hpe.software.amqp.IAmqpConnector#stopQueueConnection(java.lang.String) */ @Override public void stopQueueConnection(String consumerTag) throws InvalidOperationException, IOException { if (channel == null || consumer == null) { throw new InvalidOperationException( "Need to to connect and connectQueue before stopping a queue connection."); } channel.basicCancel(consumerTag); } /* * (non-Javadoc) * @see com.hpe.software.amqp.IAmqpConnector#deleteQueue(java.lang.String) */ @Override public void deleteQueue(String queueName) throws IOException, InvalidOperationException { if (channel == null) { throw new InvalidOperationException("Need to connect before deleting a queue."); } channel.queueDelete(queueName); } /* * (non-Javadoc) * @see com.hpe.software.amqp.IAmqpConnector#deleteExchange(java.lang.String) */ @Override public void deleteExchange(String exchangeName) throws InvalidOperationException, IOException { if (channel == null) { throw new InvalidOperationException("Need to connect before deleting an exchange."); } channel.exchangeDelete(exchangeName); } }