Skip to content

Instantly share code, notes, and snippets.

Avatar

Santiago Pericasgeertsen spericas

  • Oracle
  • Weston, FL
View GitHub Profile
View SampleResource1.java
@Path("/greet1") // CDI bean-defining annotation
@RequestScoped // implied by default
public class SampleResource1 {
private final GreetBean greetBean;
// Use @Inject instead of @Context
@Inject
private UriInfo uriInfo;
View pom.xml
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-tyrus</artifactId>
</dependency>
View pom.xml
<dependency>
<groupId>io.helidon.microprofile.websocket</groupId>
<artifactId>helidon-microprofile-websocket</artifactId>
</dependency>
View Main.java
List<Class<? extends Encoder>> encoders =
Collections.singletonList(UppercaseEncoder.class);
Routing.builder()
.register("/rest", new MessageQueueService())
.register("/websocket",
TyrusSupport.builder().register(
ServerEndpointConfig.Builder.create(
MessageBoardEndpoint.class, "/").encoders(
encoders).build()).build())
.build();
View MessageBoardEndpoint.java
public class MessageBoardEndpoint extends Endpoint {
private final MessageQueue messageQueue = MessageQueue.instance();
@Override
public void onOpen(Session session, EndpointConfig endpointConfig) {
session.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage(String message) {
try {
View MessageQueueService.java
public class MessageQueueService implements Service {
private final MessageQueue messageQueue = MessageQueue.instance();
@Override
public void update(Routing.Rules routingRules) {
routingRules.post("/board", this::handlePost);
}
private void handlePost(ServerRequest request, ServerResponse response) {
View UppercaseEncoder.java
public class UppercaseEncoder implements Encoder.Text<String> {
@Override
public String encode(String s) {
return s.toUpperCase();
}
@Override
public void init(EndpointConfig config) {
}
View MessageBoardEndpoint.java
@ServerEndpoint(
value = "/websocket",
encoders = { UppercaseEncoder.class })
public class MessageBoardEndpoint {
@Inject
private MessageQueue messageQueue;
@OnMessage
public void onMessage(Session session, String message) {
View MessageQueueResource.java
@Path("rest")
public class MessageQueueResource {
@Inject
private MessageQueue messageQueue;
@POST
@Consumes("text/plain")
public void push(String s) {
messageQueue.push(s);