Search in sources :

Example 1 with AlreadyClosedException

use of com.rabbitmq.client.AlreadyClosedException in project camel by apache.

the class RabbitMQConsumerTest method testStoppingConsumerShutdownConnectionWhenServerHasClosedChannel.

@Test
public void testStoppingConsumerShutdownConnectionWhenServerHasClosedChannel() throws Exception {
    AlreadyClosedException alreadyClosedException = Mockito.mock(AlreadyClosedException.class);
    RabbitMQConsumer consumer = new RabbitMQConsumer(endpoint, processor);
    Mockito.when(endpoint.createExecutor()).thenReturn(Executors.newFixedThreadPool(3));
    Mockito.when(endpoint.getConcurrentConsumers()).thenReturn(1);
    Mockito.when(endpoint.connect(Matchers.any(ExecutorService.class))).thenReturn(conn);
    Mockito.when(conn.createChannel()).thenReturn(channel);
    Mockito.when(channel.basicConsume(anyString(), anyBoolean(), any(Consumer.class))).thenReturn("TAG");
    Mockito.when(channel.isOpen()).thenReturn(false);
    Mockito.doThrow(alreadyClosedException).when(channel).basicCancel("TAG");
    Mockito.doThrow(alreadyClosedException).when(channel).close();
    consumer.doStart();
    consumer.doStop();
    Mockito.verify(conn).close(30 * 1000);
}
Also used : Consumer(com.rabbitmq.client.Consumer) ExecutorService(java.util.concurrent.ExecutorService) AlreadyClosedException(com.rabbitmq.client.AlreadyClosedException) Test(org.junit.Test)

Example 2 with AlreadyClosedException

use of com.rabbitmq.client.AlreadyClosedException in project cloudstack by apache.

the class RabbitMQEventBus method subscribe.

/** Call to subscribe to interested set of events
     *
     * @param topic defines category and type of the events being subscribed to
     * @param subscriber subscriber that intends to receive event notification
     * @return UUID that represents the subscription with event bus
     * @throws EventBusException
     */
@Override
public UUID subscribe(EventTopic topic, EventSubscriber subscriber) throws EventBusException {
    if (subscriber == null || topic == null) {
        throw new EventBusException("Invalid EventSubscriber/EventTopic object passed.");
    }
    // create a UUID, that will be used for managing subscriptions and also used as queue name
    // for on the queue used for the subscriber on the AMQP broker
    UUID queueId = UUID.randomUUID();
    String queueName = queueId.toString();
    try {
        String bindingKey = createBindingKey(topic);
        // store the subscriber details before creating channel
        s_subscribers.put(queueName, new Ternary(bindingKey, null, subscriber));
        // create a channel dedicated for this subscription
        Connection connection = getConnection();
        Channel channel = createChannel(connection);
        // create a queue and bind it to the exchange with binding key formed from event topic
        createExchange(channel, amqpExchangeName);
        channel.queueDeclare(queueName, false, false, false, null);
        channel.queueBind(queueName, amqpExchangeName, bindingKey);
        // register a callback handler to receive the events that a subscriber subscribed to
        channel.basicConsume(queueName, s_autoAck, queueName, new DefaultConsumer(channel) {

            @Override
            public void handleDelivery(String queueName, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                Ternary<String, Channel, EventSubscriber> queueDetails = s_subscribers.get(queueName);
                if (queueDetails != null) {
                    EventSubscriber subscriber = queueDetails.third();
                    String routingKey = envelope.getRoutingKey();
                    String eventSource = getEventSourceFromRoutingKey(routingKey);
                    String eventCategory = getEventCategoryFromRoutingKey(routingKey);
                    String eventType = getEventTypeFromRoutingKey(routingKey);
                    String resourceType = getResourceTypeFromRoutingKey(routingKey);
                    String resourceUUID = getResourceUUIDFromRoutingKey(routingKey);
                    Event event = new Event(eventSource, eventCategory, eventType, resourceType, resourceUUID);
                    event.setDescription(new String(body));
                    // deliver the event to call back object provided by subscriber
                    subscriber.onEvent(event);
                }
            }
        });
        // update the channel details for the subscription
        Ternary<String, Channel, EventSubscriber> queueDetails = s_subscribers.get(queueName);
        queueDetails.second(channel);
        s_subscribers.put(queueName, queueDetails);
    } catch (AlreadyClosedException closedException) {
        s_logger.warn("Connection to AMQP service is lost. Subscription:" + queueName + " will be active after reconnection");
    } catch (ConnectException connectException) {
        s_logger.warn("Connection to AMQP service is lost. Subscription:" + queueName + " will be active after reconnection");
    } catch (Exception e) {
        throw new EventBusException("Failed to subscribe to event due to " + e.getMessage());
    }
    return queueId;
}
Also used : EventSubscriber(org.apache.cloudstack.framework.events.EventSubscriber) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Ternary(com.cloud.utils.Ternary) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) IOException(java.io.IOException) AlreadyClosedException(com.rabbitmq.client.AlreadyClosedException) Envelope(com.rabbitmq.client.Envelope) ConfigurationException(javax.naming.ConfigurationException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ConnectException(java.net.ConnectException) EventBusException(org.apache.cloudstack.framework.events.EventBusException) IOException(java.io.IOException) AlreadyClosedException(com.rabbitmq.client.AlreadyClosedException) AMQP(com.rabbitmq.client.AMQP) Event(org.apache.cloudstack.framework.events.Event) EventBusException(org.apache.cloudstack.framework.events.EventBusException) UUID(java.util.UUID) ConnectException(java.net.ConnectException)

Example 3 with AlreadyClosedException

use of com.rabbitmq.client.AlreadyClosedException in project cloudstack by apache.

the class RabbitMQEventBus method publish.

// publish event on to the exchange created on AMQP server
@Override
public void publish(Event event) throws EventBusException {
    String routingKey = createRoutingKey(event);
    String eventDescription = event.getDescription();
    try {
        Connection connection = getConnection();
        Channel channel = createChannel(connection);
        createExchange(channel, amqpExchangeName);
        publishEventToExchange(channel, amqpExchangeName, routingKey, eventDescription);
        channel.close();
    } catch (AlreadyClosedException e) {
        closeConnection();
        throw new EventBusException("Failed to publish event to message broker as connection to AMQP broker in lost");
    } catch (Exception e) {
        throw new EventBusException("Failed to publish event to message broker due to " + e.getMessage());
    }
}
Also used : Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) EventBusException(org.apache.cloudstack.framework.events.EventBusException) AlreadyClosedException(com.rabbitmq.client.AlreadyClosedException) ConfigurationException(javax.naming.ConfigurationException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ConnectException(java.net.ConnectException) EventBusException(org.apache.cloudstack.framework.events.EventBusException) IOException(java.io.IOException) AlreadyClosedException(com.rabbitmq.client.AlreadyClosedException)

Aggregations

AlreadyClosedException (com.rabbitmq.client.AlreadyClosedException)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 Channel (com.rabbitmq.client.Channel)2 Connection (com.rabbitmq.client.Connection)2 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)2 IOException (java.io.IOException)2 ConnectException (java.net.ConnectException)2 ConfigurationException (javax.naming.ConfigurationException)2 EventBusException (org.apache.cloudstack.framework.events.EventBusException)2 Ternary (com.cloud.utils.Ternary)1 AMQP (com.rabbitmq.client.AMQP)1 Consumer (com.rabbitmq.client.Consumer)1 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)1 Envelope (com.rabbitmq.client.Envelope)1 UUID (java.util.UUID)1 ExecutorService (java.util.concurrent.ExecutorService)1 Event (org.apache.cloudstack.framework.events.Event)1 EventSubscriber (org.apache.cloudstack.framework.events.EventSubscriber)1 Test (org.junit.Test)1