List of usage examples for com.rabbitmq.client DefaultConsumer DefaultConsumer
public DefaultConsumer(Channel channel)
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//w w w .ja v a 2s. c o m 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:rabbitchat.RabbitChatClient.java
public RabbitChatClient(String host) throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(host);// w ww . ja v a2 s .co m connection = factory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_LOGS, "fanout"); channel.exchangeDeclare(EXCHANGE_USERS, "fanout"); channel.exchangeDeclare(EXCHANGE_CHANNELS, "direct"); String logsQueue = channel.queueDeclare().getQueue(); channel.queueBind(logsQueue, EXCHANGE_LOGS, ""); String usersQueue = channel.queueDeclare().getQueue(); channel.queueBind(usersQueue, EXCHANGE_USERS, ""); Consumer logsConsumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String sender = new String(body, "UTF-8"); sender = sender.substring(0, sender.indexOf(": ")); String message = new String(body, "UTF-8"); message = message.substring(message.indexOf(": ") + 2); if (!sender.equals(username)) { System.out.println(message); } } }; channel.basicConsume(logsQueue, true, logsConsumer); Consumer usersConsumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String sign = new String(body, "UTF-8").substring(0, 1); String name = new String(body, "UTF-8").substring(1); switch (sign) { case "+": users.add(name); break; case "-": users.remove(name); break; } } }; channel.basicConsume(usersQueue, true, usersConsumer); }
From source file:rabbitchat.RabbitChatClient.java
public void joinChannel(String channelName) throws IOException { if (channels.containsKey(channelName)) { System.out.println("Already a member of channel " + channelName); } else {//from w w w. j a v a2 s.c o m String channelsQueue = channel.queueDeclare().getQueue(); channel.queueBind(channelsQueue, EXCHANGE_CHANNELS, channelName); channels.put(channelName, channelsQueue); Consumer channelsConsumer = 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(message); } }; channel.basicConsume(channelsQueue, true, channelsConsumer); String message = username + ": User " + username + " joined channel " + channelName; channel.basicPublish(EXCHANGE_LOGS, "", null, message.getBytes("UTF-8")); System.out.println("Successfully joined channel " + channelName); } }
From source file:rabbitirc.RabbitClient.java
public RabbitClient() throws IOException, TimeoutException { factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); channel = connection.createChannel(); user = new User(); // channel.queueDeclare(QUEUE_NAME, false, false, false, null); channel.exchangeDeclare(NOTIFICATIONS_EX_NAME, "direct"); queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, NOTIFICATIONS_EX_NAME, "BROADCAST"); consumer = new DefaultConsumer(channel) { @Override/*from w w w. java 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(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;// ww w . ja va 2 s. c om 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.Client.java
public Client(MessageTranslator translator) throws IOException, TimeoutException { _translator = translator;/*from ww w . j a v a 2 s . c om*/ ConnectionFactory factory = new ConnectionFactory(); factory.setHost("54.69.168.245"); _connection = factory.newConnection(); _channel = _connection.createChannel(); _channel.queueDeclare(QUEUE_NAME, false, false, false, null); Consumer consumer = new DefaultConsumer(_channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { ConsumeMessage(body); } }; _channel.basicConsume(QUEUE_NAME, true, consumer); }
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);/* w ww . j ava2s . co 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);//from w w w.j a v a 2s. 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:rabbitmq_irc.Client_SendRecv.java
public Client_SendRecv(String host) { try {/* www . j a v a2 s . c o m*/ factory = new ConnectionFactory(); factory.setHost(host); connection = factory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_USER, "fanout"); channel.exchangeDeclare(EXCHANGE_MESSAGE, "topic"); String userQueue = channel.queueDeclare().getQueue(); channel.queueBind(userQueue, EXCHANGE_USER, ""); Consumer consumerUser = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String newUsername = new String(body, "UTF-8"); Client.listNick.add(newUsername); } }; channel.basicConsume(userQueue, true, consumerUser); messageQueue = channel.queueDeclare().getQueue(); } catch (IOException ex) { Logger.getLogger(Client_SendRecv.class.getName()).log(Level.SEVERE, null, ex); } catch (TimeoutException ex) { Logger.getLogger(Client_SendRecv.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:rabbitmq_irc.Client_SendRecv.java
public void joinChannel(String channelString) { try {//from ww w.j av a2 s . c o m channel.queueBind(messageQueue, EXCHANGE_MESSAGE, channelString); Consumer consumerChannel = 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(message); } }; channel.basicConsume(messageQueue, true, consumerChannel); } catch (IOException ex) { Logger.getLogger(Client_SendRecv.class.getName()).log(Level.SEVERE, null, ex); } }