README optimserver-client-stomp

Stomp client library based on ‘org.java-websocket:Java-WebSocket’ (provided for a lighter implementation; use of client-stomp-spring is recommended).

Can be used by a java client application to connect a websocket to master and be notified of job events.

!! Does not support SockJS !! (use client-stomp-spring if you need SockJS)

Installation

Maven users

Add this dependency to your project’s POM:

<dependency>
  <groupId>com.decisionbrain</groupId>
  <artifactId>optimserver-client-stomp</artifactId>
  <version>PROJECT_VERSION</version>
  <scope>compile</scope>
</dependency>

Gradle users

Add this dependency to your project’s build file:

compile "com.decisionbrain:optimserver-client-stomp:PROJECT_VERSION"
compile "org.keycloak:keycloak-authz-client:4.6.0.Final"

Getting Started

Please follow the installation instruction and execute the following Java code:

import com.decisionbrain.clientstomp.StompClientImpl;
import com.decisionbrain.optimserver.common.client.JobEventListener;
import com.decisionbrain.optimserver.common.client.stomp.StompClient;
import com.decisionbrain.optimserver.common.dto.event.SolutionDTO;
import com.decisionbrain.optimserver.common.dto.event.client.JobStatusDTO;
import com.decisionbrain.optimserver.common.dto.event.client.KpiDTO;
import com.decisionbrain.optimserver.common.dto.event.client.MessageDTO;
import com.decisionbrain.optimserver.common.dto.event.client.ProgressDTO;
import org.apache.http.impl.client.HttpClients;
import org.keycloak.authorization.client.AuthzClient;
import org.keycloak.authorization.client.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;


public class SampleJavaClient {
    private static final String KEYCLOAK_URL = "http://localhost:8081/auth";
    private static final String KEYCLOAK_REALM = "decisionbrain";
    private static final String KEYCLOAK_CLIENT = "optimserver";
    private static final String KEYCLOAK_USER = "optimserver";
    private static final String KEYCLOAK_PASSWORD = "optimserver";
    private static final String optimServerMasterHost = "localhost";
    private static final String optimServerMasterPort = "8080";

    public static void main(String[] args) {
        StompClient stompClient = new StompClientImpl("stomp+ws://" + optimServerMasterHost + ":" + optimServerMasterPort + "/os-websocket");
        stompClient.setAuthenticationToken(getToken());
        SampleListener sampleListener = new SampleListener(stompClient);
        sampleListener.registerJob("sampleJobId");
    }

    private static String getToken() {
        final Configuration configuration = new Configuration(KEYCLOAK_URL, KEYCLOAK_REALM, KEYCLOAK_CLIENT, Collections.singletonMap("secret", ""), HttpClients.createDefault());

        try {
            return AuthzClient.create(configuration).obtainAccessToken(KEYCLOAK_USER, KEYCLOAK_PASSWORD).getToken();
        } catch (Exception e) {
            throw new IllegalArgumentException("Token can't be obtained", e);
        }
    }


    private static class SampleListener implements JobEventListener {

        private Logger LOGGER = LoggerFactory.getLogger(SampleListener.class);
        private StompClient stompClient;

        public SampleListener(StompClient stompClient) {
            this.stompClient = stompClient;
        }

        @Override
        public void onStatus(JobStatusDTO status) {
            LOGGER.info("Job event received : " + status);
            if (status.getStatus().isLast()) unregisterJob(status.getJobId());
        }

        @Override
        public void onMessage(MessageDTO message) {
            LOGGER.info("Receive " + message);
        }

        @Override
        public <T> void addKpi(KpiDTO<T> kpiDTO) {
            LOGGER.info("Receive {}", kpiDTO);
        }

        @Override
        public void onProgress(ProgressDTO progress) {
            LOGGER.info("Receive " + progress);
        }

        @Override
        public void onSolution(SolutionDTO solution) {
            LOGGER.info("Receive " + solution);
        }
    }
}