List of usage examples for com.rabbitmq.client DefaultConsumer DefaultConsumer
public DefaultConsumer(Channel channel)
From source file:client.MessengerClient.java
public MessengerClient() throws IOException, TimeoutException { isLogin = false;/*from www . j ava 2s . c o m*/ factory = new ConnectionFactory(); System.out.print("input host : "); String host = sc.nextLine(); if (!host.equalsIgnoreCase("localhost")) { System.out.print("input port : "); int port = Integer.parseInt(sc.nextLine()); factory.setPort(port); } factory.setHost(host); connection = factory.newConnection(); channel = connection.createChannel(); //channel.basicQos(1); // accept only one unack-ed message at a time (see below) channel.queueDeclare(serverqueue, true, false, false, null); consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { //channel.basicAck(envelope.getDeliveryTag(), false); Message m = null; try { m = Message.toMessage(body); } catch (ClassNotFoundException ex) { Logger.getLogger(MessengerClient.class.getName()).log(Level.SEVERE, null, ex); } switch (m.getType()) { case 0: { System.out.println(m.getSender() + " : " + m.getContent()); break; } case 1: { System.out.println(m.getGroupName() + " , " + m.getSender() + " : " + m.getContent()); break; } case 2: { switch (m.getContent()) { case "joingroup": { listgroup.add(m.getGroupName()); System.out.println("join group " + m.getGroupName()); break; } case "leavegroup": { listgroup.remove(m.getGroupName()); System.out.println("leave group " + m.getGroupName()); break; } case "addfriend": { listfriend.add(m.getFriendID()); System.out.println("addfriend " + m.getFriendID()); break; } default: { System.out.println(m.getContent()); break; } } break; } } } }; autoAck = true; // acknowledgment is covered below }
From source file:co.lp.arch.TestUtils.java
public static DefaultConsumer genConsumer(Channel chan, Consumer<byte[]> c) { return new DefaultConsumer(chan) { @Override/* w w w.j a va2s. co m*/ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { c.accept(body); } }; }
From source file:com.almende.eve.transport.amqp.AmqpTransport.java
License:Apache License
@Override public void connect() throws IOException { connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(myId, true, true, true, null); Consumer consumer = new DefaultConsumer(channel) { @Override/*from w w w .ja v a2 s.c o m*/ public void handleDelivery(final String consumerTag, final Envelope envelope, final AMQP.BasicProperties properties, final byte[] body) throws IOException { final String message = new String(body, "UTF-8"); final JSONEnvelop.Envelop res = JSONEnvelop.unwrap(message); if (myId.equals(res.getTo())) { ThreadPool.getPool().execute(new Runnable() { @Override public void run() { getHandle().get().receive(res.getMessage(), URIUtil.create("amqp:" + res.getFrom()), null); } }); } } }; channel.basicConsume(myId, true, consumer); }
From source file:com.audaexplore.claimservices.listeners.FnolMessageListener.java
public void listen() { DefaultConsumer consumer = new DefaultConsumer(channel) { int ctr = 0; @Override//from w ww . j a v a 2 s .c o m public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); ctr++; System.out.println(ctr + " red'd " + message); log.info("calling persist"); persist(message); } }; try { channel.basicConsume("fnol", true, consumer); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } /*boolean autoAck = false; int ctr = 0; try { channel.basicConsume("fnol", autoAck, "myConsumerTag", new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { long deliveryTag = envelope.getDeliveryTag(); String message = new String(body, "UTF-8"); ctr++; System.out.println(ctr + " red'd " + message); log.info("calling persist"); persist(message); channel.basicAck(deliveryTag, false); } }); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ }
From source file:com.caucho.v5.pipe.rabbit.RabbitPipeImpl.java
License:Open Source License
@Override protected void onInitReceive() { if (_consumerTag != null) { return;//from ww w. j a va2 s. c o m } try { boolean isAutoAck = false; _consumerTag = _channel.basicConsume(_config.routingKey(), isAutoAck, new DefaultConsumer(_channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { RabbitMessage msg = RabbitMessage.newMessage().body(body).properties(properties) .redeliver(envelope.isRedeliver()); long deliveryTag = envelope.getDeliveryTag(); _self.onRabbitReceive(msg, (Void, e) -> { if (e != null) { _channel.basicReject(deliveryTag, false); } else { _channel.basicAck(deliveryTag, false); } }); } }); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.chat.UI.java
private void forNahid() throws IOException { Consumer consumer1 = new DefaultConsumer(channel) { @Override// www.j a va 2 s . 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 + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_ALL)) { ALL_MSG += message + "\n"; jTextArea1.setText(ALL_MSG); } } }; Consumer consumer2 = 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 + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_NAHID_SHOSHI)) { NAHID_SHOSHI_MSG += message + "\n"; jTextArea6.setText(NAHID_SHOSHI_MSG); } } }; Consumer consumer3 = 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 + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_TANNEE_NAHID)) { TANNEE_NAHID_MSG += message + "\n"; jTextArea5.setText(TANNEE_NAHID_MSG); } } }; channel.basicConsume(Config.Q_NAHID_ALL, true, consumer1); channel.basicConsume(Config.Q_NAHID_SHOSHI, true, consumer2); channel.basicConsume(Config.Q_NAHID_TANNEE, true, consumer3); }
From source file:com.chat.UI.java
private void forTannee() throws IOException { Consumer consumer1 = new DefaultConsumer(channel) { @Override//from w w w . j av a2 s .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 + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_ALL)) { ALL_MSG += message + "\n"; jTextArea1.setText(ALL_MSG); } } }; Consumer consumer2 = 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 + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_TANNEE_SHOSHI)) { TANNEE_SHOSHI_MSG += message + "\n"; jTextArea5.setText(TANNEE_SHOSHI_MSG); } } }; Consumer consumer3 = 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 + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_TANNEE_NAHID)) { TANNEE_NAHID_MSG += message + "\n"; jTextArea6.setText(TANNEE_NAHID_MSG); } } }; channel.basicConsume(Config.Q_TANNEE_ALL, true, consumer1); channel.basicConsume(Config.Q_TANNEE_SHOSHI, true, consumer2); channel.basicConsume(Config.Q_TANNEE_NAHID, true, consumer3); }
From source file:com.chat.UI.java
private void forShoshi() throws IOException { Consumer consumer1 = new DefaultConsumer(channel) { @Override/* w ww. j a v a 2 s . 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 + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_ALL)) { ALL_MSG += message + "\n"; jTextArea1.setText(ALL_MSG); } } }; Consumer consumer2 = 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 + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_NAHID_SHOSHI)) { NAHID_SHOSHI_MSG += message + "\n"; jTextArea6.setText(NAHID_SHOSHI_MSG); } } }; Consumer consumer3 = 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 + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_TANNEE_SHOSHI)) { TANNEE_SHOSHI_MSG += message + "\n"; jTextArea5.setText(TANNEE_SHOSHI_MSG); } } }; channel.basicConsume(Config.Q_SHOSHI_ALL, true, consumer1); channel.basicConsume(Config.Q_SHOSHI_TANNEE, true, consumer3); channel.basicConsume(Config.Q_SHOSHI_NAHID, true, consumer2); }
From source file:com.digispherecorp.enterprise.rabbitmq.ra.work.PublishSubcribeRabbitMQWork.java
@Override public void run() { try {/* w w w . ja va2 s . co m*/ Logger.getLogger(QueueRabbitMQWork.class.getName()).log(Level.INFO, "Work Schedule Polling started @ ".concat(uuid)); if (ipsm == null) { try { Class<?> className = Class .forName(((RabbitMQActivationSpec) activationSpec).getDestinationType()); ipsm = (IPublishSubscribeMessage) className.getConstructor().newInstance(); } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { Logger.getLogger(BootStrapRabbitMQWork.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex); } } final Channel channel; RabbitMQConnectionFactoryFacade instance = RabbitMQConnectionFactoryFacade.getInstance(); if (instance.getConnectionRequestInfo(activationSpec) == null) { instance.setConnectionRequestInfo(activationSpec, new RabbitMQConnectionRequestInfo(((RabbitMQActivationSpec) activationSpec).getUser(), ((RabbitMQActivationSpec) activationSpec).getPassword(), ((RabbitMQActivationSpec) activationSpec).getHost(), ((RabbitMQActivationSpec) activationSpec).getPort(), ((RabbitMQActivationSpec) activationSpec).getVirtualHost())); } try { connection = instance.getConnection(); channel = connection.createChannel(); channel.exchangeDeclare(((RabbitMQActivationSpec) activationSpec).getExchangeName(), ipsm.getSubscribeType(), isExchangeDurabe(ipsm)); final Consumer consumer = new DefaultConsumer(channel) { @Override public void handleConsumeOk(String consumerTag) { super.handleConsumeOk(consumerTag); } @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { super.handleDelivery(consumerTag, envelope, properties, body); if (!envelope.isRedeliver()) { try { RabbitMQMessage mQMessage = new RabbitMQMessage(); mQMessage.getMessages().add(body); try { receiveMessages(mQMessage); } catch (ResourceException ex) { Logger.getLogger(QueueRabbitMQWork.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex); } } finally { channel.basicAck(envelope.getDeliveryTag(), true); } } } }; channel.basicConsume(((RabbitMQActivationSpec) activationSpec).getQueueName(), false, consumer); } catch (IOException ex) { Logger.getLogger(RabbitMQResourceAdapter.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex); } } catch (Exception ex) { Logger.getLogger(QueueRabbitMQWork.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex); } }
From source file:com.digispherecorp.enterprise.rabbitmq.ra.work.QueueRabbitMQWork.java
@Override public void run() { try {/*from w w w .ja v a 2 s .c o m*/ Logger.getLogger(QueueRabbitMQWork.class.getName()).log(Level.INFO, "Work Schedule Polling started @ ".concat(uuid)); final Channel channel; RabbitMQConnectionFactoryFacade instance = RabbitMQConnectionFactoryFacade.getInstance(); if (instance.getConnectionRequestInfo(activationSpec) == null) { instance.setConnectionRequestInfo(activationSpec, new RabbitMQConnectionRequestInfo(((RabbitMQActivationSpec) activationSpec).getUser(), ((RabbitMQActivationSpec) activationSpec).getPassword(), ((RabbitMQActivationSpec) activationSpec).getHost(), ((RabbitMQActivationSpec) activationSpec).getPort(), ((RabbitMQActivationSpec) activationSpec).getVirtualHost())); } try { connection = instance.getConnection(); channel = connection.createChannel(); channel.queueDeclarePassive(((RabbitMQActivationSpec) activationSpec).getQueueName()); channel.basicQos(0); final Consumer consumer = new DefaultConsumer(channel) { @Override public void handleConsumeOk(String consumerTag) { super.handleConsumeOk(consumerTag); } @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { super.handleDelivery(consumerTag, envelope, properties, body); if (!envelope.isRedeliver()) { try { RabbitMQMessage mQMessage = new RabbitMQMessage(); mQMessage.getMessages().add(body); try { receiveMessages(mQMessage); } catch (ResourceException ex) { Logger.getLogger(QueueRabbitMQWork.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex); } } finally { channel.basicAck(envelope.getDeliveryTag(), true); } } } }; channel.basicConsume(((RabbitMQActivationSpec) activationSpec).getQueueName(), false, consumer); } catch (IOException ex) { Logger.getLogger(RabbitMQResourceAdapter.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex); } } catch (Exception ex) { Logger.getLogger(QueueRabbitMQWork.class.getName()).log(Level.SEVERE, ex.getLocalizedMessage(), ex); } }