0
@Singleton @Path("broadcast") public class UserCreatedEventsResource {
private Sse sse;
private SseBroadcaster broadcaster;

@Inject
public UserCreatedEventsResource(@Context final Sse sse) {
    this.sse = sse;
    this.broadcaster = sse.newBroadcaster();
}

@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
public String broadcastMessage(String message) {
    final OutboundSseEvent event = sse.newEventBuilder()
            .name("message")
            .mediaType(MediaType.TEXT_PLAIN_TYPE)
            .data(String.class, message)
            .build();

    broadcaster.broadcast(event);

    return "Message '" + message + "' has been broadcast.";
}

@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
public void listenToBroadcast(@Context SseEventSink eventSink) {
    this.broadcaster.register(eventSink);
} }

I had a nullpointer exception, but after reimporting the projekt with maven it dissappeared.

I think I did the rebuild once before but it did not work, and now I did it again and its working fine.

oneoe
  • 53
  • 1
  • 2
  • 8
  • Can you please post the stacktrace? – Govinda Sakhare May 29 '18 at 10:53
  • 1. java.lang.NullPointerException 2. java.lang.IllegalStateException: Unable to perform operation: create on se.group.projektarbete.web.UserCreatedEventsResource ] with root cause java.lang.NullPointerException: null at se.group.projektarbete.web.UserCreatedEventsResource.(UserCreatedEventsResource.java:24) ~[classes/:na] – oneoe May 29 '18 at 11:00
  • My problem is the subscribe method, i cant use it , do I need some special dependecy for it? I think I am getting the error because I am using a .register(eventSink) on the broadcaster instead. – oneoe May 29 '18 at 11:01
  • Do you have "jersey-media-sse" in your dependencies? And could you post ResourceConfig or configuration here. – mallikarjun May 29 '18 at 11:02
  • Yes I do have that dependency. – oneoe May 29 '18 at 11:05
  • "AT"Configuration public class JerseyConfig extends ResourceConfig { public JerseyConfig() { packages("se.group.projektarbete.web"); } "AT"Bean public ObjectMapper objectMapper() { return new ObjectMapper().registerModule(new ParameterNamesModule()); } } – oneoe May 29 '18 at 11:10
  • When I reimported the project with maven, the nullpointer disapeared. I had to add "AT"Inject on the constructor for the code to run. But I got a different error now 2018-05-29 13:13:15.273 ERROR 5380 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[.[.g.p.c.JerseyConfig] : Allocate exception for servlet [se.group.projektarbete.config.JerseyConfig] javax.validation.ValidationException: HV000183: Unable to initialize 'javax.el.ExpressionFactory'. Check that you have the EL dependencies on the classpath, or use ParameterMessageInterpolator instead – oneoe May 29 '18 at 11:29

0 Answers0