Example usage for com.rabbitmq.client Channel getConnection

List of usage examples for com.rabbitmq.client Channel getConnection

Introduction

In this page you can find the example usage for com.rabbitmq.client Channel getConnection.

Prototype

Connection getConnection();

Source Link

Document

Retrieve the connection which carries this channel.

Usage

From source file:net.roboconf.messaging.internal.RabbitMqTestUtils.java

License:Apache License

/**
 * A method to check whether RabbitMQ is rabbitMqIsRunning or not.
 * <p>/*  w ww.  ja va 2s  . com*/
 * Tests that must be skipped if it is not rabbitMqIsRunning must begin with
 * <code>
 * Assume.assumeTrue( rabbitMqIsRunning );
 * </code>
 * </p>
 */
public static boolean checkRabbitMqIsRunning() throws Exception {

    boolean rabbitMqIsRunning = false;
    Channel channel = null;
    try {
        channel = createTestChannel();
        Object o = channel.getConnection().getServerProperties().get("version");

        String version = String.valueOf(o);
        if (!isVersionGOEThreeDotTwo(version)) {
            Logger logger = Logger.getLogger(RabbitMqTestUtils.class.getName());
            logger.warning("Tests are skipped because RabbitMQ must be at least in version 3.2.x.");

        } else {
            rabbitMqIsRunning = true;
        }

    } catch (Exception e) {
        Logger logger = Logger.getLogger(RabbitMqTestUtils.class.getName());
        logger.warning("Tests are skipped because RabbitMQ is not rabbitMqIsRunning.");
        Utils.logException(logger, e);

    } finally {
        if (channel != null) {
            channel.close();
            channel.getConnection().close();
        }
    }

    return rabbitMqIsRunning;
}

From source file:net.roboconf.messaging.internal.utils.RabbitMqUtils.java

License:Apache License

/**
 * Closes the connection to a channel./*from   w  w  w. java 2s .  c o  m*/
 * @param channel the channel to close (can be null)
 * @throws IOException if something went wrong
 */
public static void closeConnection(Channel channel) throws IOException {

    if (channel != null) {
        if (channel.isOpen()) {
            // channel.basicCancel( consumerTag );
            channel.close();
        }

        if (channel.getConnection().isOpen())
            channel.getConnection().close();
    }
}

From source file:net.roboconf.messaging.internal.utils.RabbitMqUtilsTest.java

License:Apache License

@Test
public void testCloseConnection() throws Exception {
    Assume.assumeTrue(this.rabbitMqIsRunning);

    Channel channel = createTestChannel();
    Assert.assertTrue(channel.isOpen());
    Assert.assertTrue(channel.getConnection().isOpen());

    // Close it/*from  w  ww  .  j a  va2  s  . c  om*/
    RabbitMqUtils.closeConnection(channel);
    Assert.assertFalse(channel.isOpen());
    Assert.assertFalse(channel.getConnection().isOpen());

    // Make sure closing an already closed channel does not throw an exception
    RabbitMqUtils.closeConnection(channel);
    Assert.assertFalse(channel.isOpen());
    Assert.assertFalse(channel.getConnection().isOpen());
}

From source file:net.roboconf.messaging.rabbitmq.internal.utils.RabbitMqTestUtils.java

License:Apache License

/**
 * A method to check whether RabbitMQ is rabbitMqIsRunning or not.
 * <p>//from  w w  w .j a v a2 s  . c om
 * Tests that must be skipped if it is not rabbitMqIsRunning must begin with
 * <code>
 * Assume.assumeTrue( rabbitMqIsRunning );
 * </code>
 * </p>
 */
public static boolean checkRabbitMqIsRunning(String messageServerIp, String username, String password) {

    Logger logger = Logger.getLogger(RabbitMqTestUtils.class.getName());
    boolean rabbitMqIsRunning = false;
    Channel channel = null;
    try {
        channel = createTestChannel(messageServerIp, username, password);
        Object o = channel.getConnection().getServerProperties().get("version");

        String version = String.valueOf(o);
        if (!isVersionGOEThreeDotTwo(version)) {
            logger.warning("Tests are skipped because RabbitMQ must be at least in version 3.2.x.");

        } else {
            rabbitMqIsRunning = true;
        }

    } catch (Exception e) {
        logger.warning("Tests are skipped because RabbitMQ is not rabbitMqIsRunning.");
        Utils.logException(logger, e);

    } finally {
        try {
            if (channel != null) {
                channel.close();
                channel.getConnection().close();
            }

        } catch (Exception e) {
            Utils.logException(logger, e);
        }
    }

    return rabbitMqIsRunning;
}

From source file:net.roboconf.messaging.rabbitmq.internal.utils.RabbitMqUtils.java

License:Apache License

/**
 * Closes the connection to a channel./* ww  w  .ja va  2  s.  c o m*/
 * @param channel the channel to close (can be null)
 * @throws IOException if something went wrong
 */
public static void closeConnection(Channel channel) throws IOException {

    if (channel != null) {
        if (channel.isOpen())
            channel.close();

        if (channel.getConnection().isOpen())
            channel.getConnection().close();
    }
}

From source file:net.roboconf.messaging.rabbitmq.internal.utils.RabbitMqUtilsTest.java

License:Apache License

@Test
public void testCloseConnection() throws Exception {
    Assume.assumeTrue(rabbitMqIsRunning);

    Channel channel = RabbitMqTestUtils.createTestChannel();
    Assert.assertTrue(channel.isOpen());
    Assert.assertTrue(channel.getConnection().isOpen());

    // Close it/*from  w  w  w . ja v  a2s .  c o m*/
    RabbitMqUtils.closeConnection(channel);
    Assert.assertFalse(channel.isOpen());
    Assert.assertFalse(channel.getConnection().isOpen());

    // Make sure closing an already closed channel does not throw an exception
    RabbitMqUtils.closeConnection(channel);
    Assert.assertFalse(channel.isOpen());
    Assert.assertFalse(channel.getConnection().isOpen());
}

From source file:org.apache.airavata.gfac.monitor.impl.push.amqp.AMQPMonitor.java

License:Apache License

@Subscribe
public boolean unRegisterListener(MonitorID monitorID) throws AiravataMonitorException {
    Iterator<MonitorID> iterator = finishQueue.iterator();
    MonitorID next = null;//from  ww  w  .ja  va 2  s.  c om
    while (iterator.hasNext()) {
        next = iterator.next();
        if (next.getJobID().endsWith(monitorID.getJobID())) {
            break;
        }
    }
    if (next == null) {
        logger.error("Job has removed from the queue, old obsolete message recieved");
        return false;
    }
    String channelID = CommonUtils.getChannelID(next);
    if (JobState.FAILED.equals(monitorID.getStatus()) || JobState.COMPLETE.equals(monitorID.getStatus())) {
        finishQueue.remove(next);

        // if this is the last job in the queue at this point with the same username and same host we
        // close the channel and close the connection and remove it from availableChannels
        if (CommonUtils.isTheLastJobInQueue(finishQueue, next)) {
            logger.info("There are no jobs to monitor for common ChannelID:" + channelID
                    + " , so we unsubscribe it" + ", incase new job created we do subscribe again");
            Channel channel = availableChannels.get(channelID);
            if (channel == null) {
                logger.error("Already Unregistered the listener");
                throw new AiravataMonitorException("Already Unregistered the listener");
            } else {
                try {
                    channel.queueUnbind(channel.queueDeclare().getQueue(), "glue2.computing_activity",
                            CommonUtils.getRoutingKey(next));
                    channel.close();
                    channel.getConnection().close();
                    availableChannels.remove(channelID);
                } catch (IOException e) {
                    logger.error("Error unregistering the listener");
                    throw new AiravataMonitorException("Error unregistering the listener");
                }
            }
        }
    }
    next.setStatus(monitorID.getStatus());
    JobIdentifier jobIdentity = new JobIdentifier(next.getJobID(), next.getTaskID(), next.getWorkflowNodeID(),
            next.getExperimentID(), next.getJobExecutionContext().getGatewayID());
    publisher.publish(new JobStatusChangeEvent(next.getStatus(), jobIdentity));
    return true;
}

From source file:org.apache.airavata.gfac.monitor.impl.push.amqp.UnRegisterWorker.java

License:Apache License

@Subscribe
private boolean unRegisterListener(JobStatusChangeEvent jobStatus, MonitorID monitorID)
        throws AiravataMonitorException {
    String channelID = CommonUtils.getChannelID(monitorID);
    if (JobState.FAILED.equals(jobStatus.getState()) || JobState.COMPLETE.equals(jobStatus.getState())) {
        Channel channel = availableChannels.get(channelID);
        if (channel == null) {
            logger.error("Already Unregistered the listener");
            throw new AiravataMonitorException("Already Unregistered the listener");
        } else {//  ww  w.  java  2s  .co m
            try {
                channel.queueUnbind(channel.queueDeclare().getQueue(), "glue2.computing_activity",
                        CommonUtils.getRoutingKey(monitorID));
                channel.close();
                channel.getConnection().close();
                availableChannels.remove(channelID);
            } catch (IOException e) {
                logger.error("Error unregistering the listener");
                throw new AiravataMonitorException("Error unregistering the listener");
            }
        }
    }
    return true;
}

From source file:org.apache.nutch.indexer.history.HistoryIndexer.java

License:Apache License

private void closeChannelAndConnection(@Nullable final Channel channel, final boolean swallowErrors) {

    if (channel == null) {
        return;/*from  w  w w .  j ava2s . c om*/
    }

    try {
        final Connection connection = channel.getConnection();
        if (channel.isOpen()) {
            channel.close();
        }
        if (connection != null && connection.isOpen()) {
            connection.close();
        }
    } catch (IOException | TimeoutException e) {
        final String errMsg = "failed closing connection or channel";
        LOG.error(errMsg, e);
        if (!swallowErrors) {
            throw new RuntimeException(errMsg, e);
        }
    }
}

From source file:org.ballerinalang.messaging.rabbitmq.nativeimpl.channel.listener.Stop.java

License:Open Source License

@Override
public void execute(Context context) {
    @SuppressWarnings(RabbitMQConstants.UNCHECKED)
    BMap<String, BValue> channelListObject = (BMap<String, BValue>) context.getRefArgument(0);
    @SuppressWarnings(RabbitMQConstants.UNCHECKED)
    BMap<String, BValue> channelOb = (BMap<String, BValue>) channelListObject
            .get(RabbitMQConstants.CHANNEL_REFERENCE);
    Channel channel = (Channel) channelOb.getNativeData(RabbitMQConstants.CHANNEL_NATIVE_OBJECT);
    if (channel == null) {
        throw new BallerinaException("ChannelListener not properly initialised");
    } else {/*w  ww. j  ava  2  s . co  m*/
        try {
            Connection connection = channel.getConnection();
            channel.close();
            connection.close();
        } catch (IOException | TimeoutException exception) {
            RabbitMQUtils.returnError(RabbitMQConstants.CLOSE_CHANNEL_ERROR + " " + exception.getMessage(),
                    context, exception);
        }
    }
}