im.dadoo.logger.client.impl.DefaultLoggerClient.java Source code

Java tutorial

Introduction

Here is the source code for im.dadoo.logger.client.impl.DefaultLoggerClient.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 im.dadoo.logger.client.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import im.dadoo.log.Log;
import im.dadoo.logger.client.LoggerClient;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author codekitten
 */
public class DefaultLoggerClient implements LoggerClient {

    private static final Logger logger = LoggerFactory.getLogger(DefaultLoggerClient.class);

    private final CloseableHttpAsyncClient httpClient;

    private final HttpPost post;

    private final ObjectMapper mapper;

    public DefaultLoggerClient(String host) {
        this.httpClient = HttpAsyncClients.createDefault();
        this.post = new HttpPost(host);
        this.post.setHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        this.mapper = new ObjectMapper();
    }

    @Override
    public void send(final Log log) {
        logger.info(String.format("??,:%s", log.toString()));
        try {
            this.post.setEntity(new StringEntity(this.mapper.writeValueAsString(log)));
            if (!this.httpClient.isRunning()) {
                this.httpClient.start();
            }
            this.httpClient.execute(this.post, new FutureCallback<HttpResponse>() {
                @Override
                public void completed(HttpResponse response) {
                    HttpEntity entity = response.getEntity();
                    try {
                        String rs = EntityUtils.toString(entity);
                        logger.info(rs);
                        Boolean result = mapper.readValue(rs, Boolean.class);
                        if (result) {
                            logger.info(String.format(",?:%s", log.toPropertyString()));
                        } else {
                            logger.warn(
                                    String.format(",?:%s", log.toPropertyString()));
                        }
                    } catch (IOException ex) {
                        logger.error(String.format("IO,?:%s", log.toPropertyString()),
                                ex);
                    } catch (ParseException ex) {
                        logger.error(String.format("???,?:%s",
                                log.toPropertyString()), ex);
                    } finally {
                        EntityUtils.consumeQuietly(entity);
                    }
                }

                @Override
                public void failed(Exception ex) {
                    logger.error(String.format("??,?:%s", log.toPropertyString()), ex);
                }

                @Override
                public void cancelled() {
                    logger.warn(String.format("???,?:%s", log.toPropertyString()));
                }
            });
        } catch (JsonProcessingException ex) {
            logger.error(
                    String.format("??json,?:%s", log.toPropertyString()),
                    ex);
        } catch (UnsupportedEncodingException ex) {
            logger.error(String.format("????,?:%s", log.toPropertyString()), ex);
        }
    }

    @Override
    public void close() {
        try {
            this.httpClient.close();
        } catch (IOException ex) {
            logger.error("", ex);
        }
    }
}