Last active
April 3, 2024 01:59
-
-
Save mirzaakhena/c75caa8529e648b7fc7b4becd4ca6fe6 to your computer and use it in GitHub Desktop.
Server Sent Events Java Client Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mirzaakhena.websocketdemo; | |
import java.util.concurrent.TimeUnit; | |
import javax.annotation.PostConstruct; | |
import javax.ws.rs.client.Client; | |
import javax.ws.rs.client.ClientBuilder; | |
import javax.ws.rs.client.WebTarget; | |
import javax.ws.rs.sse.InboundSseEvent; | |
import javax.ws.rs.sse.SseEventSource; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.core.env.Environment; | |
@SpringBootApplication | |
public class SSE { | |
Logger logger = LoggerFactory.getLogger(SSE.class); | |
@Autowired | |
private Environment env; | |
public static void main(String[] args) { | |
SpringApplication.run(SSE.class, args); | |
} | |
private final String PROP_HANDSHAKING_URL = "app.handshaking_url"; | |
private final String PROP_HANDSHAKING_RETRY_IN_SECOND = "app.handshaking_retry_in_second"; | |
private WebTarget tut; | |
private SseEventSource sse; | |
@PostConstruct | |
private void initAplikasi() { | |
Client client = ClientBuilder.newClient(); | |
this.tut = client.target(env.getProperty(PROP_HANDSHAKING_URL)); | |
doHandshaking(); | |
} | |
private void doHandshaking() { | |
logger.info(String.format("Handshaking...")); | |
sse = SseEventSource.target(this.tut).build();// | |
sse.register(this::onMessage, this::onError, this::onComplete); | |
sse.open(); | |
} | |
private void onMessage(InboundSseEvent event) { | |
logger.info(String.format("Message : %s | %s", event.getName(), event.readData())); | |
} | |
private void onError(Throwable t) { | |
logger.error("Error : %s", t.getMessage()); | |
} | |
private void onComplete() { | |
logger.info(String.format("Connection Closed!")); | |
try { | |
int retry = Integer.parseInt(env.getProperty(PROP_HANDSHAKING_RETRY_IN_SECOND)); | |
Thread.sleep(retry * 1000L); | |
} catch (InterruptedException e) { | |
} | |
doHandshaking(); | |
} | |
} | |
// application.properties | |
// ----------------------- | |
// server.port = 8080 | |
// app.handshaking_url = http://localhost:3000/handshake/0208 | |
// app.handshaking_retry_in_second = 5 | |
// pom.xml dependency | |
// <dependencies> | |
// <dependency> | |
// <groupId>org.springframework.boot</groupId> | |
// <artifactId>spring-boot-starter-web</artifactId> | |
// </dependency> | |
// <dependency> | |
// <groupId>org.glassfish.jersey.media</groupId> | |
// <artifactId>jersey-media-sse</artifactId> | |
// <version>2.26</version> | |
// </dependency> | |
// <dependency> | |
// <groupId>org.glassfish.jersey.inject</groupId> | |
// <artifactId>jersey-hk2</artifactId> | |
// <version>2.26</version> | |
// </dependency> | |
// </dependencies> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment