Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Fungsi untuk subscribe data RMQ
- * @param handler
- * @param subscribeThread
- */
- public void subscribeNotification(final Handler handler, Thread subscribeThread)
- {
- subscribeThread = new Thread(new Runnable() {
- @Override
- public void run() {
- while(true) {
- try {
- Connection connection = factory.newConnection();
- Channel channel = connection.createChannel();
- channel.basicQos(0);
- channel.queueBind("panwaslu_notification", "amq.fanout", "*");
- QueueingConsumer consumer = new QueueingConsumer(channel);
- channel.basicConsume("panwaslu_notification", true, consumer);
- QueueingConsumer.Delivery delivery = consumer.nextDelivery();
- if (delivery != null){
- try{
- String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
- Log.d("ConsumeDataRMQ", "MessageConsumed" + message);
- Message msg = handler.obtainMessage();
- Bundle bundle = new Bundle();
- bundle.putString("msg", message);
- msg.setData(bundle);
- handler.sendMessage(msg);
- channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
- }catch (Exception e){
- channel.basicReject(delivery.getEnvelope().getDeliveryTag(),true);
- }
- }
- } catch (InterruptedException e) {
- break;
- } catch (Exception e1) {
- Log.d("", "Connection broken: " + e1.getClass().getName());
- try {
- Thread.sleep(4000); //sleep and then try again
- } catch (InterruptedException e) {
- break;
- }
- }
- }
- }
- });
- subscribeThread.start();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement