Java tutorial
/** * Copyright 2013 Suresh Reddy Guntaka * * 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 io.udvi.amqp.mq.api; import io.udvi.amqp.mq.broker.endpoint.UdviMQEndpointDriver; import io.udvi.amqp.mq.transport.connection.CAMQPConnectionFactory; import io.udvi.amqp.mq.transport.connection.CAMQPConnectionInterface; import io.udvi.amqp.mq.transport.connection.CAMQPConnectionManager; import io.udvi.amqp.mq.transport.connection.CAMQPConnectionProperties; import io.udvi.amqp.mq.transport.session.CAMQPSessionFactory; import io.udvi.amqp.mq.transport.session.CAMQPSessionInterface; import org.apache.commons.lang.StringUtils; /** * This class is used to initialize and shutdown UdviMQ runtime. * Also used as a factory to create AMQP connections and sessions. * * @author tejdas */ public final class ConnectionFactory { private volatile static String endpointId = null; public static String getEndpointId() { return endpointId; } /** * Initialize UdviMQ runtime with a supplied endpointId. The endpointId * allows the runtime to be distinguishable from other UdviMQ endpoints on * the same machine, and also uniquely addressable. * * @param endpointID */ public static void initialize(String endpointID) { if (endpointId == null) { if (StringUtils.isEmpty(endpointID)) { throw new IllegalArgumentException("Null EndpointID specified"); } UdviMQEndpointDriver.initialize(endpointID); endpointId = CAMQPConnectionManager.getContainerId(); } } /** * AMQP is a peer-to-peer messaging transport protocol. UdviMQ * provides capability for AMQP to be used in a peer-to-peer manner. In this * case, there is no Broker involved. Messages are sent between two AMQP * peers on a UdviMQ channel, that encapsulates an AMQP link. * * For UdviMQ to work in the peer-to-peer manner, one endpoint acts * as a UdviMQ listener, listening on a TCP port. This method provides the * capability to initialize UdviMQ runtime for an AMQP (listener) endpoint * in a Broker-less (peer-to-peer) interaction. * * This method lets the user register a ChannelEndpointListener with the endpoint. * When a UdviMQ peer creates a Channel to this endpoint, the * ChannelEndpointListener is called back with a ChannelEndpoint, that encapsulate * an AMQP link receiver. * * @param endpointID: allows the runtime to be distinguishable from other * UdviMQ endpoints on the same machine, and also uniquely addressable. * * @param listenPort: TCP port that the UdviMQ endpoint listens on. * * @param endpointListener: ChannelEndpointListener registered to receive * callback when an AMQP link is created. */ public static void initializeEndpoint(String endpointID, int listenPort, ChannelEndpointListener endpointListener) { if (endpointId == null) { if (StringUtils.isEmpty(endpointID)) { throw new IllegalArgumentException("Null EndpointID specified"); } if (endpointListener == null) { throw new IllegalArgumentException("Null ChannelEndpointListener specified"); } UdviMQEndpointDriver.initializeEndpoint(listenPort, endpointID, endpointListener); endpointId = CAMQPConnectionManager.getContainerId(); } } /** * AMQP is a peer-to-peer messaging transport protocol. UdviMQ * provides capability for AMQP to be used in a peer-to-peer manner. In this * case, there is no Broker involved. Messages are sent between two AMQP * peers on a UdviMQ channel, that encapsulates an AMQP link. * * For UdviMQ to work in the peer-to-peer manner, one endpoint acts * as a UdviMQ listener, an another endpoint acts as a client. After * an AMQP session is established, there is no distinction between the * client, and listener, i.e, they become peers and either endpoint can * initiate an AMQP link on the underlying (bidirectional) AMQP session. * * This method provides the capability to initialize UdviMQ runtime for an * AMQP client endpoint in a Broker-less (peer-to-peer) interaction. * * This method lets the user register a ChannelEndpointListener with the endpoint. * When a UdviMQ peer creates a Channel to this endpoint, the * ChannelEndpointListener is called back with a ChannelEndpoint, that encapsulate * an AMQP link receiver. * * @param endpointID: allows the runtime to be distinguishable from other * UdviMQ endpoints on the same machine, and also uniquely addressable. * * @param endpointListener: ChannelEndpointListener registered to receive * callback when an AMQP link is created. */ public static void initializeClientEndpoint(String endpointID, ChannelEndpointListener endpointListener) { if (endpointId == null) { if (StringUtils.isEmpty(endpointID)) { throw new IllegalArgumentException("Null EndpointID specified"); } if (endpointListener == null) { throw new IllegalArgumentException("Null ChannelEndpointListener specified"); } UdviMQEndpointDriver.initializeClientEndpoint(endpointID, endpointListener); endpointId = CAMQPConnectionManager.getContainerId(); } } /** * Shuts down UdviMQ runtime. */ public static void shutdown() { if (endpointId == null) { throw new IllegalStateException("UdviMQ Runtime has not been initialized yet"); } boolean isBroker = false; UdviMQEndpointDriver.shutdown(isBroker); endpointId = null; } /** * Creates a new Connection to the target UdviMQ broker. Internally, it creates * an AMQP connection. * * @param targetUdviMQBrokerAddress * @return Connection, that encapsulates an AMQP connection */ public static Connection createConnection(String targetUdviMQBrokerAddress) { if (StringUtils.isEmpty(targetUdviMQBrokerAddress)) { throw new IllegalArgumentException("UdviMQ Broker address must be specified"); } if (endpointId == null) { throw new IllegalStateException("UdviMQ Runtime has not been initialized yet"); } String brokerContainerId = String.format("broker@%s", targetUdviMQBrokerAddress); CAMQPConnectionProperties connectionProps = CAMQPConnectionProperties.createConnectionProperties(); CAMQPConnectionInterface connection = CAMQPConnectionFactory.createCAMQPConnection(brokerContainerId, connectionProps); return new Connection(connection); } public static Connection createConnectionToAMQPEndpoint(String targetUdviMQEndpointAddress, int port) { if (StringUtils.isEmpty(targetUdviMQEndpointAddress)) { throw new IllegalArgumentException("AMQP Endpoint address must be specified"); } if (endpointId == null) { throw new IllegalStateException("UdviMQ Runtime has not been initialized yet"); } String endpointContainerId = String.format("broker@%s", targetUdviMQEndpointAddress); CAMQPConnectionProperties connectionProps = CAMQPConnectionProperties.createConnectionProperties(); CAMQPConnectionInterface connection = CAMQPConnectionFactory .createCAMQPConnectionToEndpoint(endpointContainerId, port, connectionProps); return new Connection(connection); } /** * Creates a new Session to the target UdviMQ broker. Internally, it creates * an AMQP connection. It then creates an AMQP session over it. * * @param targetUdviMQBrokerAddress * @return Session, that encapsulates an AMQP session */ public static Session createSession(String targetUdviMQBrokerAddress) { if (StringUtils.isEmpty(targetUdviMQBrokerAddress)) { throw new IllegalArgumentException("UdviMQ Broker address must be specified"); } if (endpointId == null) { throw new IllegalStateException("UdviMQ Runtime has not been initialized yet"); } String brokerContainerId = String.format("broker@%s", targetUdviMQBrokerAddress); CAMQPSessionInterface camqpSession = CAMQPSessionFactory.createCAMQPSession(brokerContainerId); return new Session(endpointId, camqpSession); } }