List of usage examples for com.rabbitmq.client Channel close
@Override void close() throws IOException, TimeoutException;
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 . j a va 2 s.c o m*/ * 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 .ja v a2 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.basicCancel( consumerTag ); channel.close(); } if (channel.getConnection().isOpen()) channel.getConnection().close(); } }
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 ww w.j av a 2 s . c o m * 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./*w w w . j a va 2s .c om*/ * @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:org.apache.airavata.datacat.agent.org.apache.airavata.datacat.agent.messageBroker.RabbitMQConsumerTest.java
License:Apache License
/** * Test method to publish dummy data to RabbitMQ * @param messageType/* w w w.j av a2 s . c o m*/ * @throws java.io.IOException * @throws TException */ public void publish(MessageType messageType) throws java.io.IOException, TException { //establishing the connection ConnectionFactory factory = new ConnectionFactory(); factory.setHost(RABBITMQ_HOST); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String DATA_ROOT = AgentProperties.getInstance().getProperty(Constants.DATA_ROOT, ""); //creating the output created Event ExperimentOutputCreatedEvent event = new ExperimentOutputCreatedEvent(); event.setExperimentId("test"); event.setOutputPath(DATA_ROOT + "/2H2OOHNCmin.com.out"); //serializing the event byte[] body = ThriftUtils.serializeThriftObject(event); Message message = new Message(); message.setEvent(body); message.setMessageId("sad"); message.setMessageType(messageType); message.setUpdatedTime(993344232); String routingKey = "*"; //serializing the message object byte[] messageArray = ThriftUtils.serializeThriftObject(message); channel.basicPublish(EXCHANGE_NAME, BINDING_KEY, null, messageArray); logger.debug(" [x] Sent '" + message + "'"); channel.close(); connection.close(); }
From source file:org.apache.airavata.gfac.monitor.impl.push.amqp.AMQPMonitor.java
License:Apache License
public void run() { // before going to the while true mode we start unregister thread startRegister = true; // this will be unset by someone else while (startRegister || !ServerSettings.isStopAllThreads()) { try {//from ww w .j a v a 2 s. c o m MonitorID take = runningQueue.take(); this.registerListener(take); } catch (AiravataMonitorException e) { // catch any exceptino inside the loop e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (Exception e) { e.printStackTrace(); } } Set<String> strings = availableChannels.keySet(); for (String key : strings) { Channel channel = availableChannels.get(key); try { channel.close(); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } }
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 www . j a va 2 s.com*/ 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 {//from w w w.j av a 2s . c o 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.airavata.wsmg.client.amqp.rabbitmq.AMQPBroadcastSenderImpl.java
License:Apache License
public void Send(OMElement message) throws AMQPException { try {//from w ww .java 2s. com if (isRoutable(message)) { Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_FANOUT, AMQPUtil.EXCHANGE_TYPE_FANOUT); channel.basicPublish(AMQPUtil.EXCHANGE_NAME_FANOUT, "", null, message.toString().getBytes()); channel.close(); connection.close(); } } catch (IOException e) { throw new AMQPException(e); } }
From source file:org.apache.airavata.wsmg.client.amqp.rabbitmq.AMQPSenderImpl.java
License:Apache License
public void Send(OMElement message) throws AMQPException { try {//from ww w . j a va 2s.c om if (isRoutable(message)) { Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_DIRECT, AMQPUtil.EXCHANGE_TYPE_DIRECT); List<String> routingKeys = new ArrayList<String>(); getRoutingKeys(message, routingKeys); for (String routingKey : routingKeys) { channel.basicPublish(AMQPUtil.EXCHANGE_NAME_DIRECT, routingKey, null, message.toString().getBytes()); } channel.close(); connection.close(); } } catch (IOException e) { throw new AMQPException(e); } }