Example usage for com.rabbitmq.client Connection close

List of usage examples for com.rabbitmq.client Connection close

Introduction

In this page you can find the example usage for com.rabbitmq.client Connection close.

Prototype

@Override
void close() throws IOException;

Source Link

Document

Close this connection and all its channels with the com.rabbitmq.client.AMQP#REPLY_SUCCESS close code and message 'OK'.

Usage

From source file:localdomain.localhost.RabbitMQServer.java

License:Apache License

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {//from ww w  .j a  v  a2 s.  c o  m
        String message = request.getParameter("message");

        ConnectionFactory factory = new ConnectionFactory();
        String uri = System.getProperty("CLOUDAMQP_URL");
        factory.setUri(uri);

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        boolean durable = true;
        channel.queueDeclare(QUEUE_NAME, durable, false, false, null);

        channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");

        channel.close();
        connection.close();

        response.sendRedirect(request.getContextPath() + "/index.jsp");
    } catch (Exception e) {
        request.setAttribute("throwable", e);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
}

From source file:main.TestMain.java

public static void sendMessage(String message) throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    factory.setUsername("student");
    factory.setPassword("cph");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);

    channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
    System.out.println(" [x] Sent '" + message + "'");

    channel.close();//  w w w .j  a v  a2  s .  c  o m
    connection.close();
}

From source file:mains.RaspiSensorModule_0.java

public static void main(String[] argv) throws Exception {

    //________ Basic subscribe direct (Routing) __________
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);/* www .  j a  v a  2  s .  c  om*/
    factory.setUsername("piadmin");
    factory.setPassword("3828");
    Connection connection = factory.newConnection();
    System.setProperty("java.rmi.server.hostname", thisIp);

    ControllerPublisher conPub = new ControllerPublisher(connection, NODE_ID);
    LED_Controller ledCon = new LED_Controller("ledControllerRealTest");
    LCD_Controller lcdCon = new LCD_Controller("lcdController_messeges");

    //   NodeController ledCon1 = new NodeController("ledControllerFloor");
    //   NodeController ledCon2 = new NodeController("ledControllerCabin");

    conPub.publishController(ledCon);
    conPub.publishController(lcdCon);

    //     conPub.publishController(ledCon1);
    //   conPub.publishController(ledCon2);

    NodeSensorHandler nodeSensors = new NodeSensorHandler(connection, SENSOR_EXCHANGE,
            SENSOR_ROUT_KEY_BASE + NODE_ID);
    System.out.println("Sensors up");
    //        BusRMIConnector rmiConnection = new BusRMIConnector(connection, SENSOR_EXCHANGE, SENSOR_ROUT_KEY_BASE + NODE_ID);
    //        System.out.println("RMI up");

    System.in.read();

    connection.close();
}

From source file:messaging.Worker.java

License:Apache License

public static void sendMessage(String message) throws java.io.IOException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(ConfigurationLoader.getInstance().getRabbitmqNodename());
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);

    channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
    System.out.println(" [x] Sent '" + message + "'");

    channel.close();/*  ww w .j  av  a2  s .  co  m*/
    connection.close();
}

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 ava  2s  .co 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);//from ww  w .  j a  v  a  2  s  . com
    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();
    }//from www  .j a va2 s  .  co  m
    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.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();/*from   w  w w.j a  v a 2 s .c om*/
    connection.close();
}

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  2s.  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();
    }
}

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//from  w ww . j a  v  a 2s . 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();
}