com.wss.qvh.log.ConsumerThread.java Source code

Java tutorial

Introduction

Here is the source code for com.wss.qvh.log.ConsumerThread.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wss.qvh.log;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.ShutdownSignalException;
import com.wss.log.Constant;
import com.wss.log.LogObject;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import org.apache.log4j.Logger;

/**
 *
 * @author apple
 */
public class ConsumerThread implements Runnable {
    private final Logger logger = Logger.getLogger(ConsumerThread.class.getName());
    private final String command;

    public ConsumerThread(String s) {
        this.command = s;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " Start consumer");
        try {
            consumer();
        } catch (IOException | TimeoutException ex) {
            logger.warn(ex.getMessage(), ex);
        }
        System.out.println(Thread.currentThread().getName() + " End consumer");
    }

    public void consumer() throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername(Constant.RABBITMQ_USERNAME);
        factory.setPassword(Constant.RABBITMQ_PASSWORD);
        factory.setVirtualHost(Constant.RABBITMQ_VIRTUAL_HOST);
        factory.setRequestedChannelMax(Constant.NUM_PRODUCER);
        factory.setHost(Constant.RABBITMQ_HOST);

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

        channel.queueDeclare(Constant.QUEUE_NAME, Constant.QUEUE_DURABLE, false, false, null);
        channel.basicQos(Constant.QUEUE_PREFETCH);
        QueueingConsumer consumer = new QueueingConsumer(channel);
        boolean autoAck = false;
        channel.basicConsume(Constant.QUEUE_NAME, autoAck, consumer);

        while (true) {
            QueueingConsumer.Delivery delivery;
            try {
                delivery = consumer.nextDelivery();
            } catch (InterruptedException | ShutdownSignalException | ConsumerCancelledException ex) {
                logger.warn(ex.getMessage(), ex);
                try {
                    Thread.sleep(100L);
                } catch (InterruptedException ex1) {
                    logger.warn(ex1.getMessage(), ex1);
                }
                continue;
            }

            String message;
            try {
                message = getMessenge(delivery);
            } catch (InterruptedException ex) {
                logger.warn(ex.getMessage(), ex);
                try {
                    Thread.sleep(100L);
                } catch (InterruptedException ex1) {
                    logger.warn(ex1.getMessage(), ex1);
                }
                continue;
            }
            if (message == null || message.isEmpty()) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    logger.warn(ex.getMessage(), ex);
                }
            } else {
                LogObject lo = new LogObject();
                try {
                    lo.parseJson(message);
                    store(lo);
                    channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
                } catch (Exception ex) {
                    logger.debug(message, ex);
                }
            }
        }
    }

    public String getMessenge(QueueingConsumer.Delivery delivery) throws InterruptedException {
        return new String(delivery.getBody());
    }

    public void store(LogObject lo) throws Exception {
        lo.insert();
    }
}