home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (WebReporter.java)

This example Java source code file (WebReporter.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

dl4j, entity, exception, instance, linkedblockingqueue, logger, override, pair, reporterthread, response, runnable, threading, threads, webreporter

The WebReporter.java Java example source code

package org.deeplearning4j.ui;

import lombok.NonNull;
import org.deeplearning4j.berkeley.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * This is simple wrapper for sending state updates generated by IterationListeners.
 * Basic idea is simple: network processing should be handled in background, without slowing caller thread
 *
 * @author raver119@gmail.com
 */
public class WebReporter {
    private static final WebReporter INSTANCE = new WebReporter();
    protected LinkedBlockingQueue<Pair queue = new LinkedBlockingQueue<>();

    private static final Logger log = LoggerFactory.getLogger(WebReporter.class);

    private WebReporter() {
        ReporterThread thread = new ReporterThread(queue);
        thread.start();
    }

    public static WebReporter getInstance() {
        return INSTANCE;
    }

    /**
     * This method queues UI report for sending
     *
     * @param target
     * @param entity
     */
    public void queueReport(WebTarget target, Entity entity) {
        queue.add(Pair.makePair(target, entity));
    }

    /**
     * This method immediately sends UI report to specified target using POST request
     *
     * @param target
     * @param entity
     */
    public void postReport(WebTarget target, Entity entity) {
        Response resp = target.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(entity);
        log.debug("{}",resp);
    }

    private class ReporterThread extends Thread implements Runnable {
        private LinkedBlockingQueue<Pair queue;
        public ReporterThread(@NonNull LinkedBlockingQueue<Pair queue) {
            this.queue = queue;
            this.setName("DL4j Ui WebReporter thread");
            this.setDaemon(true);
        }

        @Override
        public void run() {
            while (true) {
                try {
                    Pair<WebTarget, Entity> pair = queue.take();
                    postReport(pair.getFirst(), pair.getSecond());
                } catch (Exception e) {
                    log.error("Exception caught but ignored: " + e.getMessage());
                    e.printStackTrace();
                } finally {
                    try {
                        Thread.sleep(100);
                    } catch (Exception e) {
                        ;
                    }
                }
            }
        }
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java WebReporter.java source code file:



my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.