Example usage for com.rabbitmq.client Connection createChannel

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

Introduction

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

Prototype

Channel createChannel() throws IOException;

Source Link

Document

Create a new channel, using an internally allocated channel number.

Usage

From source file:pubsub.RecieveLogs.java

public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, EXCHANGE_NAME, "");

    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        @Override//  ww w.  j av a 2  s .c om
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:rabbitirc.RabbitIRC.java

public static void main(String[] argv) throws java.io.IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Random rnd = new Random();
    int indeks = rnd.nextInt(usernamelist.length);
    String Randusername = usernamelist[indeks] + rnd.nextInt(100);

    user = Randusername;/*www.  j  a  v  a2 s  . co m*/
    channellist.add("lounge");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    String queueName = channel.queueDeclare().getQueue();
    channel.exchangeDeclare(EXCHANGE_NAME, "direct");

    Scanner sc = new Scanner(System.in);

    channel.queueBind(queueName, EXCHANGE_NAME, "lounge");
    channellist.add("lounge");
    Consumer consumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            System.out.println(" [x] Received :'" + message + "'");
        }
    };
    System.out.println(" Welcome " + user + " to Channel lounge");
    System.out.println(" Queries: ");
    System.out.println(" 1. /NICK <Username>            : Change username ");
    System.out.println(" 2. /JOIN <Channel Name>        : Join Channel");
    System.out.println(" 3. @<Channel Name> <Message>   : Send message to Spesific Channel");
    System.out.println(" 4. /LEAVE <Channel Name>       : Leave Channel");
    System.out.println(" 5. <Random text>               : BroadCast");
    System.out.println(" 6. /EXIT                       : Exit App");
    while (true) {
        channel.basicConsume(queueName, true, consumer);
        String input = sc.nextLine();
        String[] query;
        if ((query = CommandRegexes.NICK.match(input)) != null) {
            String Nickname = query[0];
            user = Nickname;
            System.out.println(" [x] Your Nickname '" + Nickname + "'");

        } else if ((query = CommandRegexes.JOIN.match(input)) != null) {
            String channel_name = query[0];
            channel.queueBind(queueName, EXCHANGE_NAME, channel_name);
            channellist.add(channel_name);
            System.out.println(" [x] you had Join '" + channel_name + "'");

        } else if ((query = CommandRegexes.LEAVE.match(input)) != null) {
            String channel_name = query[0];
            if (channellist.contains(channel_name)) {
                channellist.remove(channellist.indexOf(channel_name));
                channel.queueUnbind(queueName, EXCHANGE_NAME, channel_name);
                System.out.println(" [x] you had leave '" + channel_name);
            } else {
                System.out.println(" [x] you're not in the channel " + channel_name);
            }
            System.out.println(" [x] you had leave '" + channel_name + "'");

        } else if (CommandRegexes.EXIT.match(input) != null) {
            channel.close();
            connection.close();
            break;

        } else if ((query = CommandRegexes.MESSAGE_CHANNEL.match(input)) != null) {
            String channel_name = query[0];
            if (channellist.contains(channel_name)) {
                String message = "[" + channel_name + "] (" + user + ") " + query[1];
                channel.basicPublish(EXCHANGE_NAME, channel_name, null, message.getBytes());
                System.out.println(" [x] Sent '" + message + "'");
                System.out.println(" : to '" + channel_name + "'");
            } else {
                System.out.println(" [x] No Channel " + channel_name + " on your list");
            }
        } else {
            System.out.println(" [x] Broadcasting '" + input + "'");
            for (String channellist1 : channellist) {
                String messages = "[" + channellist1 + "] (" + user + ") " + input;
                channel.basicPublish(EXCHANGE_NAME, channellist1, null, messages.getBytes());
            }
            System.out.println(" [x] OK ;D");
        }
    }

}

From source file:rabbitmq.Producer.java

public void run() {
    try {/* w  w w .ja v  a  2s.c o  m*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

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

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

        for (int i = 0; i < num; i++) {
            Mensaje persona = new Mensaje("RabbitMQ_Java", "Persona" + i, "ID" + i);
            Gson gson = new Gson();
            String message = gson.toJson(persona);

            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            System.out.println(" [" + id + "] Sent '" + message + "'");
        }

        channel.close();
        connection.close();
    } catch (IOException ex) {
        Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:rabbitmqapp.EmitLog.java

public static void main(String... args) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUri(Cons.RABBIT_MQ_URL);
    factory.setConnectionTimeout(3000);//from  ww  w. j  a v  a  2s . c o m
    factory.setRequestedHeartbeat(30);

    //Create a connection
    Connection connection = factory.newConnection();
    //create a channel from connection
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(Cons.EXCHANGE_NAME, EXCHANGE_TYPE);
    String message = "Hello Dolly";

    channel.basicPublish(Cons.EXCHANGE_NAME, "", null, message.getBytes());
    System.out.println(" [x] Sent '" + message + "'");
    channel.close();
    connection.close();
}

From source file:rabbitmqapp.RabbitMQApp.java

/**
 * @param args the command line arguments
 *///from w  w w.  j ava 2s.  com
public static void main(String[] args) throws Exception {
    ProductOrder order = new ProductOrder("Thando Mlauzi", 34.50);
    ConnectionFactory factory = new ConnectionFactory();
    String uri = "amqp://ytsoedex:Qu2LCiBJ5x9fhRUyLYkMhJqsURJ9dkSP@chicken.rmq.cloudamqp.com/ytsoedex";
    factory.setUri(uri);

    //Recommended settings
    factory.setRequestedHeartbeat(30);
    factory.setConnectionTimeout(30000);

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

    channel.queueDeclare(QUENAME, false, false, false, null);
    String message = "Hello World!";
    channel.basicPublish("", QUENAME, null, RabbitUtility.convertToByteArray(order));
    System.out.println(" [x] Sent '" + order + "'");

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

}

From source file:rabbitmqapp.Recv.java

public static void main(String... args) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    String uri = "amqp://ytsoedex:Qu2LCiBJ5x9fhRUyLYkMhJqsURJ9dkSP@chicken.rmq.cloudamqp.com/ytsoedex";
    factory.setUri(uri);/*from   w  w  w. j a v a 2  s.c  o  m*/

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

    channel.queueDeclare(QUENAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            System.out.println(String.format("The consumer tag is %s", consumerTag));
            ProductOrder message = (ProductOrder) RabbitUtility.getObject(body);

            //String message = new String(body, "UTF-8");
            System.out.println(" [x] Received name'" + message.getName() + "'");
            System.out.println(" [x] Received price'" + message.getPrice() + "'");
        }
    };
    channel.basicConsume(QUENAME, true, consumer);
}

From source file:rabbitmq_clienttest.Simple_receiver.java

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

    db = new Database_connector_sqlite();

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setUsername("es");
    factory.setPassword("a");
    //factory.setVirtualHost("/");
    factory.setPort(5672);/*w  ww  . ja v a  2 s. c  o  m*/
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
            doWork(message);

        }
    };
    channel.basicConsume(QUEUE_NAME, true, consumer);

}

From source file:raspisensormodule.BrokerSubscriber.java

public BrokerSubscriber() {
    this.foo = 1;

    try {//ww w  .  j a va 2  s .c o m

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        sensorChannel = connection.createChannel();

        sensorChannel.exchangeDeclare(SENSOR_EXCHANGE, "topic");

        String sensChName = sensorChannel.queueDeclare().getQueue();
        sensorChannel.queueBind(sensChName, SENSOR_EXCHANGE, BIND_KEY_ALL_SENSOR);
        sensorChannel.basicConsume(sensChName, true, sensorConsumer);
        System.out.println("Subscriber up");

    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:raspisensormodule.NodeSensorHandler.java

public NodeSensorHandler(Connection busConnection, String exchange, String baseRoutingKey) throws Exception {
    super(busConnection, exchange, baseRoutingKey);

    initSensors();//from  w  w w  . j  a v a2 s . com

    Channel channelTemp = null;
    Channel channelPress = null;
    Channel channelLight = null;

    try {
        channelTemp = busConnection.createChannel();
        channelTemp.exchangeDeclare(exchange, "topic");
        channelPress = busConnection.createChannel();
        channelPress.exchangeDeclare(exchange, "topic");
        channelLight = busConnection.createChannel();
        channelLight.exchangeDeclare(exchange, "topic");
    } catch (IOException ex) {
        Logger.getLogger(NodeSensorHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
    NodeSensor tempUpdater = new NodeSensor(6000, tempHumSensors, channelTemp, exchange,
            baseRoutingKey + ".temp", this);
    Thread t = new Thread(tempUpdater);
    addThread(t);

    NodeSensor pressUpdater = new NodeSensor(3000, pressureSensors, channelPress, exchange,
            baseRoutingKey + ".pressure", this);
    t = new Thread(pressUpdater);
    addThread(t);

    NodeSensor lightUpdater = new NodeSensor(12000, lightSensors, channelLight, exchange,
            baseRoutingKey + ".light", this);
    t = new Thread(lightUpdater);
    addThread(t);

}

From source file:reactor.rabbitmq.ChannelProxy.java

License:Open Source License

public ChannelProxy(Connection connection) throws IOException {
    this.connection = connection;
    this.delegate = connection.createChannel();
}