README optimserver-client-stomp-spring

Stomp client library based on ‘org.springframework:spring-websocket’.

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

!! This implementation supports SockJS !! (client-stomp doesn’t)

Installation

Maven users

Add this dependency to your project’s POM:

<dependency>
  <groupId>com.decisionbrain</groupId>
  <artifactId>optimserver-client-stomp-spring</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-spring: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.optimserver.client.stomp.spring.annotation.EnableOptimServerClient;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Collections;

@SpringBootApplication
@EnableOptimServerClient
@ComponentScan({"com.decisionbrain.optimserver.client.java.resttemplate.api",
        "com.decisionbrain.optimserver.client.stomp.spring",
        "com.decisionbrain.optimserver.client.spring.service"})
public class OptimserverSampleSpringClientApplication {
    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";

    public static void main(String[] args) {
        SpringApplication.run(OptimserverSampleSpringClientApplication.class, args);
    }


    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);
        }
    }

    @Service
    public class SampleJavaClient implements CommandLineRunner {
        private final StompClient stompClient;
        private final SampleListener sampleListener;

        @Autowired
        public SampleJavaClient(StompClient stompClient,
                                SampleListener sampleListener) {
            this.stompClient = stompClient;
            this.stompClient.setAuthenticationToken(getToken());
            this.sampleListener = sampleListener;
        }

        @Override
        public void run(String... args) {
            sampleListener.registerJob("sampleJobId");
        }
    }

    @Component
    public 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);
        }
    }
}