0

First of all I want to tell that this question is more about CDI, especially CDI Events. Container ( in my case Weblogic 12 is not able to inject the Event object)

Motivation for the Question:

I am trying to integrate JMS with WebSocket. Basically I am trying to consume a message from JMS and then raise an event so that a server endpoint can receive the JMS message and ultimately raise it to the browser through the WebSocket protocol. I have got the idea to integrate between JMS and WebSocket from this blog. Please note that I am not using CDI for any other purpose.. But I am facing an NPE when I try to fire the injected event.

My understanding is that to use CDI I need to define a beans.xml, even if almost empty.

My beans.xml(I have put this one inside inside WEB-INF directory of my web-app and I have pasted the contents below:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
  http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

My EventProducer class looks like the following:

public class EventProducer {
    @Inject
    @WSJMSMessage
    Event<DataChangeEvent> wsDeltaEvent;
    boolean raiseEvent = false;

    void produceEvent() {
        if (raiseEvent) {
            DataChangeEvent event = new DataChangeEvent(lastSeqNumberOfChangeLists, messageSelector, dataChangeEntries);
            wsDeltaEvent.fire(event); //Raising the NPE because wsDeltaEvent has not been injected.
        }
    }
}

The Event Qualifier is defined thus:

/**
 * Identifies WebSocket JMS messages.
 */
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})

public @interface WSJMSMessage {
}

The consumer of the event is the method onJMSMessage on the ServerEndpoint class and the relevant portion is pasted below:

@ServerEndpoint(value = "/{projectName}/{businessViewName}/wsdata",
                encoders = {WSJsonEncoder.class},
                decoders = {WSJsonDecoder.class})
public class WebSocketActiveDataHandler {

    // Other Lifecycle methods omitted for brevity

    public void onJMSMessage(@Observes @WSJMSMessage DataChangeEvent dataChangeEvent) {
        //Something here
    }
}

What am I missing here? Any pointers?

Geek
  • 26,489
  • 43
  • 149
  • 227
  • who's calling produceEvent()? Question behind this is 'make sure you get a hold of an EventProducer instance which is @Injected into the caller of produceEvent()' – Franck Oct 20 '15 at 15:36
  • @Franck `produceEvent()` is called from [JMS's `onMessage()` method.](https://docs.oracle.com/javaee/7/api/javax/jms/MessageListener.html) – Geek Oct 20 '15 at 16:06
  • How do you get a reference to EventProducer? Maybe post the relevant part of your client code... – Franck Oct 20 '15 at 17:32

0 Answers0