List of usage examples for com.rabbitmq.client Channel close
@Override void close() throws IOException, TimeoutException;
From source file:ms.dew.core.cluster.spi.rabbit.RabbitClusterMQ.java
License:Apache License
/** * MQ ?? ?.//from w ww.j av a 2 s .c o m * <p> * exchange = topic * * @param topic * @param routingKey Key * @param queueName ?? * @param message ? * @param confirm ?? * @return ???rabbit confirm ????? */ public boolean publishWithTopic(String topic, String routingKey, String queueName, String message, boolean confirm) { logger.trace("[MQ] publishWithTopic {}:{}", topic, message); Connection connection = rabbitAdapter.getConnection(); Channel channel = connection.createChannel(false); Object funResult = null; try { if (confirm) { channel.confirmSelect(); } channel.queueDeclare(queueName, true, false, false, null); channel.exchangeDeclare(topic, BuiltinExchangeType.TOPIC, true); AMQP.BasicProperties properties = new AMQP.BasicProperties("text/plain", null, getMQHeader(topic), 2, 0, null, null, null, null, null, null, null, null, null); funResult = sendBeforeFun.invoke(topic, routingKey, properties); channel.basicPublish(topic, routingKey, properties, message.getBytes()); if (confirm) { try { return channel.waitForConfirms(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); logger.error("[MQ] Rabbit publishWithTopic error.", e); sendErrorFun.invoke(e, funResult); return false; } } else { return true; } } catch (IOException e) { logger.error("[MQ] Rabbit publishWithTopic error.", e); sendErrorFun.invoke(e, funResult); return false; } finally { try { channel.close(); sendFinishFun.invoke(funResult); } catch (IOException | TimeoutException e) { logger.error("[MQ] Rabbit publishWithTopic error.", e); } connection.close(); } }
From source file:net.echinopsii.ariane.community.messaging.rabbitmq.ServiceFactory.java
License:Open Source License
/** * Create a new subscriber service./* w w w. j av a 2s .co m*/ * * @param baseSource the feed base source * @param selector the selector on the feed source (can be null) * @param feedWorker the feed message worker * @return the new subscriber service */ @Override public MomAkkaService subscriberService(final String baseSource, String selector, AppMsgWorker feedWorker) { MomAkkaService ret = null; ActorRef subsActor; MomConsumer consumer; final Connection connection = ((Client) super.getMomClient()).getConnection(); if (selector == null || selector.equals("")) selector = "#"; if (connection != null && connection.isOpen()) { subsActor = super.getMomClient().getActorSystem().actorOf(MsgSubsActor.props(feedWorker), baseSource + "." + ((selector.equals("#")) ? "all" : selector) + "_msgWorker"); final ActorRef runnableSubsActor = subsActor; final String select = selector; final Client cli = ((Client) super.getMomClient()); consumer = new MomConsumer() { private boolean isRunning = false; @Override public void run() { Channel channel = null; try { channel = connection.createChannel(); channel.exchangeDeclare(baseSource, "topic"); String queueName = cli.getClientID() + "_SUBS_2_" + baseSource + "." + select; channel.queueDeclare(queueName, false, true, false, null); channel.queueBind(queueName, baseSource, select); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); isRunning = true; while (isRunning) { try { QueueingConsumer.Delivery delivery = consumer.nextDelivery(10); if (delivery != null && isRunning) runnableSubsActor.tell(delivery, null); } catch (InterruptedException e) { e.printStackTrace(); } } if (channel.getConnection().isOpen()) channel.close(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (channel.getConnection().isOpen()) channel.close(); } catch (IOException e) { e.printStackTrace(); } } } @Override public boolean isRunning() { return isRunning; } @Override public void start() { new Thread(this).start(); } @Override public void stop() { isRunning = false; try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } }; consumer.start(); ret = new MomAkkaService().setMsgWorker(subsActor).setConsumer(consumer) .setClient(super.getMomClient()); super.getServices().add(ret); } return ret; }
From source file:net.es.netshell.rabbitmq.Consume.java
License:Open Source License
public void consumeMessage() throws Exception { if (queueName == null) { queueName = new UUIDManager(QUEUE_FILE).checkUUID(); }//from ww w.j a v a 2s.c o m ConnectionFactory factory = new SSLConnection(info).createConnection(); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(queueName, false, false, true, null); System.out.println(" [*] Waiting for messages."); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, "consumer", false, false, null, consumer); //while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); ByteArrayInputStream bais = new ByteArrayInputStream(delivery.getBody()); ObjectInputStream in = new ObjectInputStream(bais); DefaultListenableGraph g = (DefaultListenableGraph) in.readObject(); System.out.println(" [x] Received Message"); // GraphViewer view = new GraphViewer(g); // view.init(); // if (message.substring(0,13).equals("TOKEN_REQUEST")) { // String[] token = new ProcessTokenRequest(message, channel).sendToken(); // permissions.put(token[0], token[1]); // //String[] messageSplit = message.split(":"); // //sendToken(messageSplit[1], messageSplit[2], channel); // } else { // String[] messageSplit = message.split(":", 2); // if (permissions.containsKey(messageSplit[0])) { // System.out.println(" [x] Received '" + messageSplit[1] + "' from: " + permissions.get(messageSplit[0])); // } else { // System.out.println(" ERROR: INVALID TOKEN PROVIDED IN MESSAGE"); // } // } //} channel.queueDelete(queueName); channel.close(); connection.close(); }
From source file:net.es.netshell.rabbitmq.Publish.java
License:Open Source License
public void Publish(Object object) throws Exception { ByteArrayOutputStream os = new ByteArrayOutputStream(); ObjectOutput objOut = new ObjectOutputStream(os); objOut.writeObject(object);//ww w .ja va 2 s . c o m byte byteForm[] = os.toByteArray(); objOut.close(); os.close(); if (queueName == null) { queueName = new UUIDManager(QUEUE_FILE).checkUUID(); } ConnectionFactory factory = new SSLConnection(info).createConnection(); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(queueName, false, false, true, null); channel.basicPublish("", queueName, null, byteForm); System.out.println(" [x] Sent Message"); channel.close(); connection.close(); }
From source file:net.es.netshell.rabbitmq.Publish.java
License:Open Source License
public void Publish(String[] argv) throws Exception { if (queueName == null) { queueName = new UUIDManager(QUEUE_FILE).checkUUID(); }/* www .ja v a 2 s . c om*/ String message = new ParseMessage(argv).getMessage(); ConnectionFactory factory = new SSLConnection(info).createConnection(); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); token = new CreateToken(info, channel, queueName).getToken(); message = token + ":" + message; channel.queueDeclare(queueName, false, false, true, null); channel.basicPublish("", queueName, null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); channel.close(); connection.close(); }
From source file:net.lshift.accent.AccentChannel.java
License:Apache License
/** * Closes the given accent channel. Any persist consumers will be terminated. * @throws IOException//w w w . j a va2 s .com */ @Override public void close() throws IOException { this.connection.removeConnectionListener(this); // Close our underlying connection executeIfChannelValid(new ChannelCallback() { public void runWithChannel(Channel c) throws IOException { c.close(); // Invalidate the stored channel channel = null; // Inform all listeners that the channel is lost for (ChannelListener cb : setupListeners) { cb.channelLost(); } } }); }
From source file:net.nzcorp.hbase.tableevent_signaler.TableEventSignaler.java
License:Apache License
private void publishMessage(String queueName, AMQP.BasicProperties headers, String message) throws IOException { long pmStart = System.nanoTime(); LOGGER.trace("Getting channel"); final Channel channel = getChannel(); try {//www . j a va 2s . com LOGGER.trace(String.format("Ensuring that queue: %s exists", queueName)); ensureQueue(channel, queueName); LOGGER.debug(String.format("Ensured channel in %d ms", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - pmStart))); LOGGER.trace(String.format("Sending message to queue: %s", queueName)); channel.basicPublish("", queueName, headers, message.getBytes()); // Channel seems to work. Use it again. LOGGER.debug(String.format("Sent message in %d ms", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - pmStart))); LOGGER.trace("Message sent, releasing channel"); releaseChannel(channel); LOGGER.debug(String.format("Released channel in %d ms", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - pmStart))); } catch (Throwable t) { // There was an error on the channel, throw it away. try { channel.close(); } catch (Exception e) { } LOGGER.error(String.format("Error sending message to channel: %s", queueName), t); throw t; } }
From source file:net.roboconf.messaging.internal.AbstractRabbitMqTest.java
License:Apache License
/** * A method to check whether RabbitMQ is rabbitMqIsRunning or not. * <p>//ww w . j av a 2s . com * Tests that must be skipped if it is not rabbitMqIsRunning must begin with * <code> * Assume.assumeTrue( rabbitMqIsRunning ); * </code> * </p> */ @Before public void checkRabbitMQIsRunning() throws Exception { 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(getClass().getName()); logger.warning("Tests are skipped because RabbitMQ must be at least in version 3.2.x."); this.rabbitMqIsRunning = false; } } catch (Exception e) { Logger logger = Logger.getLogger(getClass().getName()); logger.warning("Tests are skipped because RabbitMQ is not rabbitMqIsRunning."); logger.finest(Utils.writeException(e)); this.rabbitMqIsRunning = false; } finally { if (channel != null) { channel.close(); channel.getConnection().close(); } } }
From source file:net.roboconf.messaging.internal.client.MessageServerClientRabbitMq.java
License:Apache License
@Override public void cleanAllMessagingServerArtifacts() throws IOException { if (this.connected) throw new IOException("This instance is already connected to the messaging server."); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(this.messageServerIp); Connection connection = factory.newConnection(); Channel channel = this.connection.createChannel(); channel.exchangeDelete(getExchangeName(true)); channel.exchangeDelete(getExchangeName(false)); channel.close(); connection.close();/* w w w. j a va2 s. c o m*/ }
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 w w. ja v a 2 s . co 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(); } }