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

Java example source code file (StateTrackerDropWizardResource.java)

This example Java source code file (StateTrackerDropWizardResource.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

application, content-disposition, environment, exception, file, get, logger, override, path, put, response, saving, serializable, statetrackerdropwizardresource, util

The StateTrackerDropWizardResource.java Java example source code

 *  * Copyright 2015 Skymind,Inc.
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *        http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.

package org.deeplearning4j.scaleout.statetracker.hazelcast;

import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.deeplearning4j.scaleout.api.statetracker.StateTracker;
import org.deeplearning4j.util.SerializationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

 * Hazelcast resource
 * @author Adam Gibson
public class StateTrackerDropWizardResource extends Application<HazelCastConf> implements Serializable {

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

    private StateTracker stateTracker;
    private Environment env;
    public StateTrackerDropWizardResource(StateTracker stateTracker) {
        this.stateTracker = stateTracker;

    public void initialize(Bootstrap<HazelCastConf> hazelCastConfBootstrap) {

    public void run(HazelCastConf hazelCastConf, Environment environment) throws Exception {
        this.env = environment;

    public Response currMiniBatchSize() {
        return Response.ok(Collections.singletonMap("minibatch", stateTracker.miniBatchSize())).build();

    public Response numBatches() {
        return Response.ok(Collections.singletonMap("numbatches",stateTracker.numBatchesRan())).build();

    public Response setMiniBatchSizeRest(@PathParam("num") int num) {
        return Response.ok(Collections.singletonMap("status","mini batch to " + num)).build();

    public Response jobs() {
        return Response.ok(stateTracker.jobIds()).build();

    public Response currentState() {
        return Response.ok(Collections.singletonMap("phase","finetune")).build();

    public Response listWorkers() {
        return Response.ok(new ArrayList<>(stateTracker.workers())).build();

    public Response printModel() throws Exception {
        return Response.ok(Collections.singletonMap("model",stateTracker.getCurrent().toString())).build();

    public Response listWorkersSize() {
        return Response.ok(new ArrayList<>(stateTracker.workers()).size()).build();

    public Response listUpdatesSize() {
        return Response.ok(stateTracker.workerUpdates().size()).build();

    public Response getFile() throws Exception {
        Serializable u =  stateTracker.getCurrent();
        File file = new File("savedmodel.ser");
        SerializationUtils.saveObject(u, file);
        return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"" ) //optional

    public Response saveModel() {
        log.info("Saving model...");
        try {
            Serializable u = stateTracker.getCurrent();
            SerializationUtils.saveObject(u,new File("savedmodel.ser"));

        }catch(Exception e) {
            return Response.ok(Collections.singletonMap("status", e.getMessage())).build();

        return Response.ok(Collections.singletonMap("status", "saved")).build();

    public void shutdown() {
        if(env != null && env.getAdminContext() != null)



Other Java examples (source code examples)

Here is a short list of links related to this Java StateTrackerDropWizardResource.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.