List of usage examples for com.rabbitmq.client Connection createChannel
Channel createChannel() throws IOException;
From source file:net.roboconf.messaging.internal.client.MessageServerClientRabbitMqTest.java
License:Apache License
/** * A method to check whether RabbitMQ is running or not. * <p>/*from w ww . jav a 2s . c o m*/ * If it is not running, tests in this class will be skipped. * </p> */ @Before public void checkRabbitMQIsRunning() throws Exception { Assume.assumeTrue(this.running); Connection connection = null; Channel channel = null; try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(MESSAGE_SERVER_IP); connection = factory.newConnection(); channel = connection.createChannel(); } catch (Exception e) { Logger logger = Logger.getLogger(getClass().getName()); logger.warning("Tests are skipped because RabbitMQ is not running."); logger.finest(Utils.writeException(e)); this.running = false; Assume.assumeNoException(e); } finally { if (channel != null) channel.close(); if (connection != null) connection.close(); } }
From source file:OnlyPackege.TheMightyBank.java
public static void main(String[] args) throws Exception { JSONParser jsonParster = new JSONParser(); ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel recvChannel = connection.createChannel(); recvChannel.exchangeDeclare(EXCHANGE_NAME, "fanout"); String queueName = recvChannel.queueDeclare().getQueue(); recvChannel.queueBind(queueName, EXCHANGE_NAME, ""); QueueingConsumer consumer = new QueueingConsumer(recvChannel); recvChannel.basicConsume(queueName, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String responseQueue = delivery.getProperties().getReplyTo(); Channel sendChannel = connection.createChannel(); sendChannel.queueDeclare(responseQueue, false, false, false, null); String message = new String(delivery.getBody()); JSONObject recvObj = (JSONObject) jsonParster.parse(message); //to be deleted after testing System.out.println(" [x] Received '" + message + "'"); JSONObject obj = new JSONObject(); obj.put("ssn", recvObj.get("ssn")); obj.put("interestRate", Math.random() * 10); sendChannel.basicPublish("", responseQueue, null, obj.toJSONString().getBytes()); }/*from w ww . j a va 2 s . c o m*/ }
From source file:org.apache.airavata.datacat.agent.messageBroker.AiravataUpdateListener.java
License:Apache License
public void startBroker() { (new Thread(new Runnable() { @Override// www. j a v a 2s . co m public void run() { try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(RABBITMQ_HOST); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String queueName = channel.queueDeclare().getQueue(); channel.basicQos(1); channel.queueBind(queueName, EXCHANGE_NAME, BINDING_KEY); logger.debug("Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while (runFileUpdateListener) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); Message message = new Message(); ThriftUtils.createThriftFromBytes(delivery.getBody(), message); TBase event = null; if (message.getMessageType().equals(MessageType.EXPERIMENT_OUTPUT)) { ExperimentOutputCreatedEvent experimentOutputCreatedEvent = new ExperimentOutputCreatedEvent(); ThriftUtils.createThriftFromBytes(message.getEvent(), experimentOutputCreatedEvent); logger.debug(" Message Received with message id '" + message.getMessageId() + "' and with message type '" + message.getMessageType() + "' with experiment name " + experimentOutputCreatedEvent.getExperimentName()); event = experimentOutputCreatedEvent; logger.debug(" [x] Received FileInfo Message'"); process(experimentOutputCreatedEvent, message.getUpdatedTime()); logger.debug(" [x] Done Processing FileInfo Message"); } else { logger.debug("Recieved message of type ..." + message.getMessageType()); } } } catch (Exception e) { logger.error(e); } } })).start(); }
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 . ja v a 2 s . co 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
@Override public boolean registerListener(MonitorID monitorID) throws AiravataMonitorException { // we subscribe to read user-host based subscription HostDescription host = monitorID.getHost(); String hostAddress = host.getType().getHostAddress(); // in amqp case there are no multiple jobs per each host, because once a job is put in to the queue it // will be picked by the Monitor, so jobs will not stay in this queueu but jobs will stay in finishQueue String channelID = CommonUtils.getChannelID(monitorID); if (availableChannels.get(channelID) == null) { try {//w ww. j av a 2 s .co m //todo need to fix this rather getting it from a file Connection connection = AMQPConnectionUtil.connect(amqpHosts, connectionName, proxyPath); Channel channel = null; channel = connection.createChannel(); availableChannels.put(channelID, channel); String queueName = channel.queueDeclare().getQueue(); BasicConsumer consumer = new BasicConsumer(new JSONMessageParser(), localPublisher); // here we use local publisher channel.basicConsume(queueName, true, consumer); String filterString = CommonUtils.getRoutingKey(monitorID.getUserName(), hostAddress); // here we queuebind to a particular user in a particular machine channel.queueBind(queueName, "glue2.computing_activity", filterString); logger.info("Using filtering string to monitor: " + filterString); } catch (IOException e) { logger.error("Error creating the connection to finishQueue the job:" + monitorID.getUserName()); } } return true; }
From source file:org.apache.airavata.gfac.monitor.impl.push.amqp.SimpleJobFinishConsumer.java
License:Apache License
public void listen() { try {/* ww w . jav a 2 s . c o m*/ String queueName = ServerSettings.getSetting(Constants.GFAC_SERVER_PORT, "8950"); String uri = "amqp://localhost"; ConnectionFactory connFactory = new ConnectionFactory(); connFactory.setUri(uri); Connection conn = connFactory.newConnection(); logger.info("--------Created the connection to Rabbitmq server successfully-------"); final Channel ch = conn.createChannel(); logger.info("--------Created the channel with Rabbitmq server successfully-------"); ch.queueDeclare(queueName, false, false, false, null); logger.info("--------Declare the queue " + queueName + " in Rabbitmq server successfully-------"); final QueueingConsumer consumer = new QueueingConsumer(ch); ch.basicConsume(queueName, consumer); (new Thread() { public void run() { try { while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); logger.info( "---------------- Job Finish message received:" + message + " --------------"); synchronized (completedJobsFromPush) { completedJobsFromPush.add(message); } ch.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } catch (Exception ex) { logger.error("--------Cannot connect to a RabbitMQ Server--------", ex); } } }).start(); } catch (Exception ex) { logger.error("Cannot connect to a RabbitMQ Server: ", ex); logger.info("------------- Push monitoring for HPC jobs is disabled -------------"); } }
From source file:org.apache.airavata.wsmg.client.amqp.rabbitmq.AMQPBroadcastReceiverImpl.java
License:Apache License
public void Subscribe() throws AMQPException { if (callback != null) { try {//from w w w .java 2s. c o m Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_FANOUT, AMQPUtil.EXCHANGE_TYPE_FANOUT); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, AMQPUtil.EXCHANGE_NAME_FANOUT, ""); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); callback.onMessage(message); } } catch (Exception e) { throw new AMQPException(e); } } }
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 w w . ja v a 2 s. c om*/ 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.AMQPReceiverImpl.java
License:Apache License
public void Subscribe(AMQPRoutingKey key) throws AMQPException { if (callback != null) { try {//from w w w.j av a 2 s . co m Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(AMQPUtil.EXCHANGE_NAME_DIRECT, AMQPUtil.EXCHANGE_TYPE_DIRECT); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, AMQPUtil.EXCHANGE_NAME_DIRECT, key.getNativeKey()); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); callback.onMessage(message); } } catch (Exception 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 . jav a2 s . c o m*/ 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); } }