You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Luca Merolla <lu...@gmail.com> on 2012/09/25 15:33:40 UTC

CDI Injection in WebSocket is not working

Hi everyone,

I have noticed this blog entry
http://rmannibucau.wordpress.com/2012/05/15/tomee-or-how-to-use-websocket-with-cdi/
and
I have compared to my solution which is very similar but on my case since
CDI was not working I had to put some code in the init() method of my
servlet to retrieve the Beans, like this:

jsonProvider = (JsonProviderBean) getServletContext().getAttribute(
GalaxyConstants.JSON_PROVIDER_BEAN);
processor = WebSocketConnectionProcessor.getInstance(jsonProvider);

the jsonProvider was created in my ServletContextListener class and the
servlet was simply retrieving it. For the WebSocketConnectionProcessor I
opted to make the class singleton and in this way it was working.

Despite this solution, I have tried to follow the example from the blog of
Romain because I would like to inject the processor.

@WebServlet(urlPatterns = "/gametable", asyncSupported = true,
loadOnStartup = 1)
public class WebSocketGametableServlet extends WebSocketServlet implements
IWebSocketServlet {

private static final long serialVersionUID = -6573757123424131706L;

protected final Logger LOGGER = LoggerFactory.getLogger(getClass()
.getName());

@Inject
private WebSocketGametableProcessor processor;
private JsonProviderBean jsonProvider;

public void init() throws ServletException {
LOGGER.info("Initialize Lobby WebSocket servlet");
super.init();

initializeWebSocketProcessor();
LOGGER.info("Web Socket connection processor initialized");
}

/**
 * Get the JSON provider bean from the servlet context and retrieve the
 * {@link WebSocketGametableProcessor} instance
 */
private void initializeWebSocketProcessor() {
LOGGER.info("Getting the json provider bean");
jsonProvider = (JsonProviderBean) getServletContext().getAttribute(
GalaxyConstants.JSON_PROVIDER_BEAN);

// LOGGER.info("Getting the websocker processor instance");
// processor = WebSocketGametableProcessor.getInstance(jsonProvider);
}

/*
 * (non-Javadoc)
 *
 * @see
 * org.apache.catalina.websocket.WebSocketServlet#createWebSocketInbound
 * (java.lang.String, javax.servlet.http.HttpServletRequest)
 */
@Override
protected StreamInbound createWebSocketInbound(String arg0,
HttpServletRequest request) {
return new GametableMessageInbound(this,
(Long) request.getAttribute(WebSocketConstants.SESSION_ID));
}

@Override
public void addConnection(LobbyKey lobbyKey,
AbstractMessageInbound lobbyConnection) {
if (processor == null)
LOGGER.info("add connection...FAIL");
else
LOGGER.info("add connection...");
}

@Override
public void removeConnection(LobbyKey lobbyKey,
AbstractMessageInbound lobbyConnection) {
if (processor == null)
LOGGER.info("remove connection...FAIL");
else
LOGGER.info("remove connection...");
}

@Override
public void processTextCommand(String jsonCommand) {
processor.processWebsocketCommand(jsonCommand);
}
}

The WebSocketGametableProcessor has no other dependencies

public class WebSocketGametableProcessor {

private static final Logger LOGGER = LoggerFactory
.getLogger(WebSocketGametableProcessor.class);

private WebSocketGametableProcessor() {

}

public void processWebsocketCommand(String jsonCommand) {
LOGGER.info("Received json command |{}|", jsonCommand);
}
}

And as result I have that the WebSocketGametableProcessor is null in the
servlet. I was using the snapshot from last week, I have just run the
latest 1.5.1-SNAPSHOT and the issue persist.

Luca

Re: CDI Injection in WebSocket is not working

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi,

opened https://issues.apache.org/jira/browse/TOMEE-451

pushed some changes but it is still a work in progress while we didnt
validate it against the TCK suite

*Romain Manni-Bucau*
*Twitter: @rmannibucau <https://twitter.com/rmannibucau>*
*Blog: **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
*LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
*Github: https://github.com/rmannibucau*




2012/9/26 Romain Manni-Bucau <rm...@gmail.com>

> Yep
>
> You can even try the preview repo David posted some days ago
> Le 26 sept. 2012 20:42, "zeeman" <ha...@fastmail.us> a écrit :
>
> You Da man Romain. You included an Arquillian test with the project, I have
>> been struggling to get my integration tests to run. Will duplicate your
>> setup and see if that fixes the issue. I'm using 1.1 snapshot, I noticed
>> you
>> used 1.1.1 snapshot, that should not affect using Arquillian, right?
>>
>>
>>
>> --
>> View this message in context:
>> http://openejb.979440.n4.nabble.com/CDI-Injection-in-WebSocket-is-not-working-tp4657627p4657654.html
>> Sent from the OpenEJB User mailing list archive at Nabble.com.
>>
>

Re: CDI Injection in WebSocket is not working

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Yep

You can even try the preview repo David posted some days ago
Le 26 sept. 2012 20:42, "zeeman" <ha...@fastmail.us> a écrit :

> You Da man Romain. You included an Arquillian test with the project, I have
> been struggling to get my integration tests to run. Will duplicate your
> setup and see if that fixes the issue. I'm using 1.1 snapshot, I noticed
> you
> used 1.1.1 snapshot, that should not affect using Arquillian, right?
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/CDI-Injection-in-WebSocket-is-not-working-tp4657627p4657654.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Re: CDI Injection in WebSocket is not working

Posted by zeeman <ha...@fastmail.us>.
You Da man Romain. You included an Arquillian test with the project, I have
been struggling to get my integration tests to run. Will duplicate your
setup and see if that fixes the issue. I'm using 1.1 snapshot, I noticed you
used 1.1.1 snapshot, that should not affect using Arquillian, right?



--
View this message in context: http://openejb.979440.n4.nabble.com/CDI-Injection-in-WebSocket-is-not-working-tp4657627p4657654.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: CDI Injection in WebSocket is not working

Posted by Romain Manni-Bucau <rm...@gmail.com>.
That's why i created https://github.com/rmannibucau/cdi-tomcat-websocket

Btw the websocket spec is in early draft and things will soon be better
letting you annotate @websocket cdi beans or ejb.

- Romain
Le 26 sept. 2012 02:21, "zeeman" <ha...@fastmail.us> a écrit :

> Websockets is highly dependent on used container. In your servlet you need
> to
> use the right Tomcat classes.
>
> Have a look at source of Primefaces Push.
> http://www.primefaces.org/showcase-labs/push/index.jsf
> They have components that work great with websockets. It'll give you an
> example on what you need to do for Tomcat.
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/CDI-Injection-in-WebSocket-is-not-working-tp4657627p4657650.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Re: CDI Injection in WebSocket is not working

Posted by zeeman <ha...@fastmail.us>.
Websockets is highly dependent on used container. In your servlet you need to
use the right Tomcat classes.

Have a look at source of Primefaces Push.
http://www.primefaces.org/showcase-labs/push/index.jsf
They have components that work great with websockets. It'll give you an
example on what you need to do for Tomcat.



--
View this message in context: http://openejb.979440.n4.nabble.com/CDI-Injection-in-WebSocket-is-not-working-tp4657627p4657650.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: CDI Injection in WebSocket is not working

Posted by Luca Merolla <lu...@gmail.com>.
I have removed the load on startup, still it doesn't work.

At the moment, I have my code that is working without CDI due to this
issue. Is not the best but for now it can stay as it is.
I might give a try to the workaround you proposed or I'll wait for the next
release then.


On Tue, Sep 25, 2012 at 4:58 PM, Romain Manni-Bucau
<rm...@gmail.com>wrote:

> CDI + Ear still needs a big rework so i'm not totally surprised
>
> Even wonder is removing load on startup would be enough to make it working.
>
> A workaround is actually to use the whole tomee as the ear and put common
> libs in the shared classloader. That's not perfect but ear support of CDI
> is comming (i hope next spec version will handle that correctly since today
> it doesn't answer properly to it).
>
> Not sure we can hack on it before the releas, i fear it can break too much
> things and make us late for the release. I'd prefer to put it as a target
> for next release.
>
> *Romain Manni-Bucau*
> *Twitter: @rmannibucau*
> *Blog: **http://rmannibucau.wordpress.com/*<
> http://rmannibucau.wordpress.com/>
> *LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
>
>
>
>
> 2012/9/25 Luca Merolla <lu...@gmail.com>
>
> > Yes, I moved the war under webapps and there the injection works. also
> the
> > exception on deployment is gone.
> >
> >
> > On Tue, Sep 25, 2012 at 4:44 PM, Romain Manni-Bucau
> > <rm...@gmail.com>wrote:
> >
> > > hmm,
> > >
> > > can you test in a simple war please (== not an ear)?
> > >
> > > *Romain Manni-Bucau*
> > > *Twitter: @rmannibucau*
> > > *Blog: **http://rmannibucau.wordpress.com/*<
> > > http://rmannibucau.wordpress.com/>
> > > *LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
> > >
> > >
> > >
> > >
> > > 2012/9/25 Luca Merolla <lu...@gmail.com>
> > >
> > > > Here is the sample:
> > > >
> > > > *
> > > >
> > > >
> > >
> >
> http://www.fileconvoy.com/dfl.php?id=g9c64757621dc468115011126f019deb361277a
> > > > *
> > > > *
> > > > *
> > > > Deployed under apps/Test.ear it should give the following errors:
> > > >
> > > > On Deployment I get this exception: which however I have already
> > noticed
> > > in
> > > > other web app, but it doesn't seems to effect the CDI injection later
> > > when
> > > > the app is running. In the other web app I get similar exception but
> > it's
> > > > only on deployment and then the @Inject works.
> > > >
> > > > http://pastebin.com/qvqn3WsD
> > > >
> > > > After deployment, If I try to establish the websocket connection and
> I
> > > get:
> > > >
> > > > Sep 25, 2012 4:36:42 PM
> > > > org.pokernuvola.galaxy.gametable.WebSocketGametableServlet
> > > > createWebSocketInbound
> > > > INFO: CREATE connection...FAIL
> > > > Sep 25, 2012 4:36:42 PM
> > > > org.pokernuvola.galaxy.gametable.GametableMessageInbound onOpen
> > > > INFO: Opening the connection |1|
> > > > Sep 25, 2012 4:36:42 PM
> > > > org.pokernuvola.galaxy.gametable.WebSocketGametableServlet
> > > > addGametableConnection
> > > > INFO: add connection...FAIL
> > > > Sep 25, 2012 4:36:52 PM
> > > > org.pokernuvola.galaxy.gametable.GametableMessageInbound
> onTextMessage
> > > > INFO: Got a message text! |Ping|
> > > > Sep 25, 2012 4:36:52 PM
> > > > org.apache.coyote.AbstractProtocol$AbstractConnectionHandler process
> > > > SEVERE: Error reading request, ignored
> > > > java.lang.NullPointerException
> > > >         at
> > > >
> > > >
> > >
> >
> org.pokernuvola.galaxy.gametable.WebSocketGametableServlet.processTextCommand(WebSocketGametableServlet.java:74)
> > > >         at
> > > >
> > > >
> > >
> >
> org.pokernuvola.galaxy.gametable.GametableMessageInbound.onTextMessage(GametableMessageInbound.java:35)
> > > >         at
> > > >
> > > >
> > >
> >
> org.apache.catalina.websocket.MessageInbound.onTextData(MessageInbound.java:74)
> > > >         at
> > > >
> > > >
> > >
> >
> org.apache.catalina.websocket.StreamInbound.doOnTextData(StreamInbound.java:186)
> > > >         at
> > > >
> > >
> >
> org.apache.catalina.websocket.StreamInbound.onData(StreamInbound.java:134)
> > > >         at
> > > >
> > > >
> > >
> >
> org.apache.coyote.http11.upgrade.UpgradeProcessor.upgradeDispatch(UpgradeProcessor.java:83)
> > > >         at
> > > >
> > > >
> > >
> >
> org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:583)
> > > >         at
> > > >
> > > >
> > >
> >
> org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
> > > >         at
> > > >
> > > >
> > >
> >
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
> > > >         at
> > > >
> > > >
> > >
> >
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
> > > >         at java.lang.Thread.run(Thread.java:662)
> > > >
> > > >
> > > >
> > > > On Tue, Sep 25, 2012 at 3:43 PM, Romain Manni-Bucau
> > > > <rm...@gmail.com>wrote:
> > > >
> > > > > can you share the sample please?
> > > > >
> > > > > *Romain Manni-Bucau*
> > > > > *Twitter: @rmannibucau*
> > > > > *Blog: **http://rmannibucau.wordpress.com/*<
> > > > > http://rmannibucau.wordpress.com/>
> > > > > *LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > 2012/9/25 Luca Merolla <lu...@gmail.com>
> > > > >
> > > > > > Hi everyone,
> > > > > >
> > > > > > I have noticed this blog entry
> > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
> http://rmannibucau.wordpress.com/2012/05/15/tomee-or-how-to-use-websocket-with-cdi/
> > > > > > and
> > > > > > I have compared to my solution which is very similar but on my
> case
> > > > since
> > > > > > CDI was not working I had to put some code in the init() method
> of
> > my
> > > > > > servlet to retrieve the Beans, like this:
> > > > > >
> > > > > > jsonProvider = (JsonProviderBean)
> getServletContext().getAttribute(
> > > > > > GalaxyConstants.JSON_PROVIDER_BEAN);
> > > > > > processor =
> WebSocketConnectionProcessor.getInstance(jsonProvider);
> > > > > >
> > > > > > the jsonProvider was created in my ServletContextListener class
> and
> > > the
> > > > > > servlet was simply retrieving it. For the
> > > WebSocketConnectionProcessor
> > > > I
> > > > > > opted to make the class singleton and in this way it was working.
> > > > > >
> > > > > > Despite this solution, I have tried to follow the example from
> the
> > > blog
> > > > > of
> > > > > > Romain because I would like to inject the processor.
> > > > > >
> > > > > > @WebServlet(urlPatterns = "/gametable", asyncSupported = true,
> > > > > > loadOnStartup = 1)
> > > > > > public class WebSocketGametableServlet extends WebSocketServlet
> > > > > implements
> > > > > > IWebSocketServlet {
> > > > > >
> > > > > > private static final long serialVersionUID =
> -6573757123424131706L;
> > > > > >
> > > > > > protected final Logger LOGGER =
> LoggerFactory.getLogger(getClass()
> > > > > > .getName());
> > > > > >
> > > > > > @Inject
> > > > > > private WebSocketGametableProcessor processor;
> > > > > > private JsonProviderBean jsonProvider;
> > > > > >
> > > > > > public void init() throws ServletException {
> > > > > > LOGGER.info("Initialize Lobby WebSocket servlet");
> > > > > > super.init();
> > > > > >
> > > > > > initializeWebSocketProcessor();
> > > > > > LOGGER.info("Web Socket connection processor initialized");
> > > > > > }
> > > > > >
> > > > > > /**
> > > > > >  * Get the JSON provider bean from the servlet context and
> retrieve
> > > the
> > > > > >  * {@link WebSocketGametableProcessor} instance
> > > > > >  */
> > > > > > private void initializeWebSocketProcessor() {
> > > > > > LOGGER.info("Getting the json provider bean");
> > > > > > jsonProvider = (JsonProviderBean)
> getServletContext().getAttribute(
> > > > > > GalaxyConstants.JSON_PROVIDER_BEAN);
> > > > > >
> > > > > > // LOGGER.info("Getting the websocker processor instance");
> > > > > > // processor =
> > WebSocketGametableProcessor.getInstance(jsonProvider);
> > > > > > }
> > > > > >
> > > > > > /*
> > > > > >  * (non-Javadoc)
> > > > > >  *
> > > > > >  * @see
> > > > > >  *
> > > > org.apache.catalina.websocket.WebSocketServlet#createWebSocketInbound
> > > > > >  * (java.lang.String, javax.servlet.http.HttpServletRequest)
> > > > > >  */
> > > > > > @Override
> > > > > > protected StreamInbound createWebSocketInbound(String arg0,
> > > > > > HttpServletRequest request) {
> > > > > > return new GametableMessageInbound(this,
> > > > > > (Long) request.getAttribute(WebSocketConstants.SESSION_ID));
> > > > > > }
> > > > > >
> > > > > > @Override
> > > > > > public void addConnection(LobbyKey lobbyKey,
> > > > > > AbstractMessageInbound lobbyConnection) {
> > > > > > if (processor == null)
> > > > > > LOGGER.info("add connection...FAIL");
> > > > > > else
> > > > > > LOGGER.info("add connection...");
> > > > > > }
> > > > > >
> > > > > > @Override
> > > > > > public void removeConnection(LobbyKey lobbyKey,
> > > > > > AbstractMessageInbound lobbyConnection) {
> > > > > > if (processor == null)
> > > > > > LOGGER.info("remove connection...FAIL");
> > > > > > else
> > > > > > LOGGER.info("remove connection...");
> > > > > > }
> > > > > >
> > > > > > @Override
> > > > > > public void processTextCommand(String jsonCommand) {
> > > > > > processor.processWebsocketCommand(jsonCommand);
> > > > > > }
> > > > > > }
> > > > > >
> > > > > > The WebSocketGametableProcessor has no other dependencies
> > > > > >
> > > > > > public class WebSocketGametableProcessor {
> > > > > >
> > > > > > private static final Logger LOGGER = LoggerFactory
> > > > > > .getLogger(WebSocketGametableProcessor.class);
> > > > > >
> > > > > > private WebSocketGametableProcessor() {
> > > > > >
> > > > > > }
> > > > > >
> > > > > > public void processWebsocketCommand(String jsonCommand) {
> > > > > > LOGGER.info("Received json command |{}|", jsonCommand);
> > > > > > }
> > > > > > }
> > > > > >
> > > > > > And as result I have that the WebSocketGametableProcessor is null
> > in
> > > > the
> > > > > > servlet. I was using the snapshot from last week, I have just run
> > the
> > > > > > latest 1.5.1-SNAPSHOT and the issue persist.
> > > > > >
> > > > > > Luca
> > > > > >
> > > > >
> > > >
> > >
>

Re: CDI Injection in WebSocket is not working

Posted by Romain Manni-Bucau <rm...@gmail.com>.
CDI + Ear still needs a big rework so i'm not totally surprised

Even wonder is removing load on startup would be enough to make it working.

A workaround is actually to use the whole tomee as the ear and put common
libs in the shared classloader. That's not perfect but ear support of CDI
is comming (i hope next spec version will handle that correctly since today
it doesn't answer properly to it).

Not sure we can hack on it before the releas, i fear it can break too much
things and make us late for the release. I'd prefer to put it as a target
for next release.

*Romain Manni-Bucau*
*Twitter: @rmannibucau*
*Blog: **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
*LinkedIn: **http://fr.linkedin.com/in/rmannibucau*




2012/9/25 Luca Merolla <lu...@gmail.com>

> Yes, I moved the war under webapps and there the injection works. also the
> exception on deployment is gone.
>
>
> On Tue, Sep 25, 2012 at 4:44 PM, Romain Manni-Bucau
> <rm...@gmail.com>wrote:
>
> > hmm,
> >
> > can you test in a simple war please (== not an ear)?
> >
> > *Romain Manni-Bucau*
> > *Twitter: @rmannibucau*
> > *Blog: **http://rmannibucau.wordpress.com/*<
> > http://rmannibucau.wordpress.com/>
> > *LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
> >
> >
> >
> >
> > 2012/9/25 Luca Merolla <lu...@gmail.com>
> >
> > > Here is the sample:
> > >
> > > *
> > >
> > >
> >
> http://www.fileconvoy.com/dfl.php?id=g9c64757621dc468115011126f019deb361277a
> > > *
> > > *
> > > *
> > > Deployed under apps/Test.ear it should give the following errors:
> > >
> > > On Deployment I get this exception: which however I have already
> noticed
> > in
> > > other web app, but it doesn't seems to effect the CDI injection later
> > when
> > > the app is running. In the other web app I get similar exception but
> it's
> > > only on deployment and then the @Inject works.
> > >
> > > http://pastebin.com/qvqn3WsD
> > >
> > > After deployment, If I try to establish the websocket connection and I
> > get:
> > >
> > > Sep 25, 2012 4:36:42 PM
> > > org.pokernuvola.galaxy.gametable.WebSocketGametableServlet
> > > createWebSocketInbound
> > > INFO: CREATE connection...FAIL
> > > Sep 25, 2012 4:36:42 PM
> > > org.pokernuvola.galaxy.gametable.GametableMessageInbound onOpen
> > > INFO: Opening the connection |1|
> > > Sep 25, 2012 4:36:42 PM
> > > org.pokernuvola.galaxy.gametable.WebSocketGametableServlet
> > > addGametableConnection
> > > INFO: add connection...FAIL
> > > Sep 25, 2012 4:36:52 PM
> > > org.pokernuvola.galaxy.gametable.GametableMessageInbound onTextMessage
> > > INFO: Got a message text! |Ping|
> > > Sep 25, 2012 4:36:52 PM
> > > org.apache.coyote.AbstractProtocol$AbstractConnectionHandler process
> > > SEVERE: Error reading request, ignored
> > > java.lang.NullPointerException
> > >         at
> > >
> > >
> >
> org.pokernuvola.galaxy.gametable.WebSocketGametableServlet.processTextCommand(WebSocketGametableServlet.java:74)
> > >         at
> > >
> > >
> >
> org.pokernuvola.galaxy.gametable.GametableMessageInbound.onTextMessage(GametableMessageInbound.java:35)
> > >         at
> > >
> > >
> >
> org.apache.catalina.websocket.MessageInbound.onTextData(MessageInbound.java:74)
> > >         at
> > >
> > >
> >
> org.apache.catalina.websocket.StreamInbound.doOnTextData(StreamInbound.java:186)
> > >         at
> > >
> >
> org.apache.catalina.websocket.StreamInbound.onData(StreamInbound.java:134)
> > >         at
> > >
> > >
> >
> org.apache.coyote.http11.upgrade.UpgradeProcessor.upgradeDispatch(UpgradeProcessor.java:83)
> > >         at
> > >
> > >
> >
> org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:583)
> > >         at
> > >
> > >
> >
> org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
> > >         at
> > >
> > >
> >
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
> > >         at
> > >
> > >
> >
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
> > >         at java.lang.Thread.run(Thread.java:662)
> > >
> > >
> > >
> > > On Tue, Sep 25, 2012 at 3:43 PM, Romain Manni-Bucau
> > > <rm...@gmail.com>wrote:
> > >
> > > > can you share the sample please?
> > > >
> > > > *Romain Manni-Bucau*
> > > > *Twitter: @rmannibucau*
> > > > *Blog: **http://rmannibucau.wordpress.com/*<
> > > > http://rmannibucau.wordpress.com/>
> > > > *LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
> > > >
> > > >
> > > >
> > > >
> > > > 2012/9/25 Luca Merolla <lu...@gmail.com>
> > > >
> > > > > Hi everyone,
> > > > >
> > > > > I have noticed this blog entry
> > > > >
> > > > >
> > > >
> > >
> >
> http://rmannibucau.wordpress.com/2012/05/15/tomee-or-how-to-use-websocket-with-cdi/
> > > > > and
> > > > > I have compared to my solution which is very similar but on my case
> > > since
> > > > > CDI was not working I had to put some code in the init() method of
> my
> > > > > servlet to retrieve the Beans, like this:
> > > > >
> > > > > jsonProvider = (JsonProviderBean) getServletContext().getAttribute(
> > > > > GalaxyConstants.JSON_PROVIDER_BEAN);
> > > > > processor = WebSocketConnectionProcessor.getInstance(jsonProvider);
> > > > >
> > > > > the jsonProvider was created in my ServletContextListener class and
> > the
> > > > > servlet was simply retrieving it. For the
> > WebSocketConnectionProcessor
> > > I
> > > > > opted to make the class singleton and in this way it was working.
> > > > >
> > > > > Despite this solution, I have tried to follow the example from the
> > blog
> > > > of
> > > > > Romain because I would like to inject the processor.
> > > > >
> > > > > @WebServlet(urlPatterns = "/gametable", asyncSupported = true,
> > > > > loadOnStartup = 1)
> > > > > public class WebSocketGametableServlet extends WebSocketServlet
> > > > implements
> > > > > IWebSocketServlet {
> > > > >
> > > > > private static final long serialVersionUID = -6573757123424131706L;
> > > > >
> > > > > protected final Logger LOGGER = LoggerFactory.getLogger(getClass()
> > > > > .getName());
> > > > >
> > > > > @Inject
> > > > > private WebSocketGametableProcessor processor;
> > > > > private JsonProviderBean jsonProvider;
> > > > >
> > > > > public void init() throws ServletException {
> > > > > LOGGER.info("Initialize Lobby WebSocket servlet");
> > > > > super.init();
> > > > >
> > > > > initializeWebSocketProcessor();
> > > > > LOGGER.info("Web Socket connection processor initialized");
> > > > > }
> > > > >
> > > > > /**
> > > > >  * Get the JSON provider bean from the servlet context and retrieve
> > the
> > > > >  * {@link WebSocketGametableProcessor} instance
> > > > >  */
> > > > > private void initializeWebSocketProcessor() {
> > > > > LOGGER.info("Getting the json provider bean");
> > > > > jsonProvider = (JsonProviderBean) getServletContext().getAttribute(
> > > > > GalaxyConstants.JSON_PROVIDER_BEAN);
> > > > >
> > > > > // LOGGER.info("Getting the websocker processor instance");
> > > > > // processor =
> WebSocketGametableProcessor.getInstance(jsonProvider);
> > > > > }
> > > > >
> > > > > /*
> > > > >  * (non-Javadoc)
> > > > >  *
> > > > >  * @see
> > > > >  *
> > > org.apache.catalina.websocket.WebSocketServlet#createWebSocketInbound
> > > > >  * (java.lang.String, javax.servlet.http.HttpServletRequest)
> > > > >  */
> > > > > @Override
> > > > > protected StreamInbound createWebSocketInbound(String arg0,
> > > > > HttpServletRequest request) {
> > > > > return new GametableMessageInbound(this,
> > > > > (Long) request.getAttribute(WebSocketConstants.SESSION_ID));
> > > > > }
> > > > >
> > > > > @Override
> > > > > public void addConnection(LobbyKey lobbyKey,
> > > > > AbstractMessageInbound lobbyConnection) {
> > > > > if (processor == null)
> > > > > LOGGER.info("add connection...FAIL");
> > > > > else
> > > > > LOGGER.info("add connection...");
> > > > > }
> > > > >
> > > > > @Override
> > > > > public void removeConnection(LobbyKey lobbyKey,
> > > > > AbstractMessageInbound lobbyConnection) {
> > > > > if (processor == null)
> > > > > LOGGER.info("remove connection...FAIL");
> > > > > else
> > > > > LOGGER.info("remove connection...");
> > > > > }
> > > > >
> > > > > @Override
> > > > > public void processTextCommand(String jsonCommand) {
> > > > > processor.processWebsocketCommand(jsonCommand);
> > > > > }
> > > > > }
> > > > >
> > > > > The WebSocketGametableProcessor has no other dependencies
> > > > >
> > > > > public class WebSocketGametableProcessor {
> > > > >
> > > > > private static final Logger LOGGER = LoggerFactory
> > > > > .getLogger(WebSocketGametableProcessor.class);
> > > > >
> > > > > private WebSocketGametableProcessor() {
> > > > >
> > > > > }
> > > > >
> > > > > public void processWebsocketCommand(String jsonCommand) {
> > > > > LOGGER.info("Received json command |{}|", jsonCommand);
> > > > > }
> > > > > }
> > > > >
> > > > > And as result I have that the WebSocketGametableProcessor is null
> in
> > > the
> > > > > servlet. I was using the snapshot from last week, I have just run
> the
> > > > > latest 1.5.1-SNAPSHOT and the issue persist.
> > > > >
> > > > > Luca
> > > > >
> > > >
> > >
> >
>

Re: CDI Injection in WebSocket is not working

Posted by Luca Merolla <lu...@gmail.com>.
Yes, I moved the war under webapps and there the injection works. also the
exception on deployment is gone.


On Tue, Sep 25, 2012 at 4:44 PM, Romain Manni-Bucau
<rm...@gmail.com>wrote:

> hmm,
>
> can you test in a simple war please (== not an ear)?
>
> *Romain Manni-Bucau*
> *Twitter: @rmannibucau*
> *Blog: **http://rmannibucau.wordpress.com/*<
> http://rmannibucau.wordpress.com/>
> *LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
>
>
>
>
> 2012/9/25 Luca Merolla <lu...@gmail.com>
>
> > Here is the sample:
> >
> > *
> >
> >
> http://www.fileconvoy.com/dfl.php?id=g9c64757621dc468115011126f019deb361277a
> > *
> > *
> > *
> > Deployed under apps/Test.ear it should give the following errors:
> >
> > On Deployment I get this exception: which however I have already noticed
> in
> > other web app, but it doesn't seems to effect the CDI injection later
> when
> > the app is running. In the other web app I get similar exception but it's
> > only on deployment and then the @Inject works.
> >
> > http://pastebin.com/qvqn3WsD
> >
> > After deployment, If I try to establish the websocket connection and I
> get:
> >
> > Sep 25, 2012 4:36:42 PM
> > org.pokernuvola.galaxy.gametable.WebSocketGametableServlet
> > createWebSocketInbound
> > INFO: CREATE connection...FAIL
> > Sep 25, 2012 4:36:42 PM
> > org.pokernuvola.galaxy.gametable.GametableMessageInbound onOpen
> > INFO: Opening the connection |1|
> > Sep 25, 2012 4:36:42 PM
> > org.pokernuvola.galaxy.gametable.WebSocketGametableServlet
> > addGametableConnection
> > INFO: add connection...FAIL
> > Sep 25, 2012 4:36:52 PM
> > org.pokernuvola.galaxy.gametable.GametableMessageInbound onTextMessage
> > INFO: Got a message text! |Ping|
> > Sep 25, 2012 4:36:52 PM
> > org.apache.coyote.AbstractProtocol$AbstractConnectionHandler process
> > SEVERE: Error reading request, ignored
> > java.lang.NullPointerException
> >         at
> >
> >
> org.pokernuvola.galaxy.gametable.WebSocketGametableServlet.processTextCommand(WebSocketGametableServlet.java:74)
> >         at
> >
> >
> org.pokernuvola.galaxy.gametable.GametableMessageInbound.onTextMessage(GametableMessageInbound.java:35)
> >         at
> >
> >
> org.apache.catalina.websocket.MessageInbound.onTextData(MessageInbound.java:74)
> >         at
> >
> >
> org.apache.catalina.websocket.StreamInbound.doOnTextData(StreamInbound.java:186)
> >         at
> >
> org.apache.catalina.websocket.StreamInbound.onData(StreamInbound.java:134)
> >         at
> >
> >
> org.apache.coyote.http11.upgrade.UpgradeProcessor.upgradeDispatch(UpgradeProcessor.java:83)
> >         at
> >
> >
> org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:583)
> >         at
> >
> >
> org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
> >         at
> >
> >
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
> >         at
> >
> >
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
> >         at java.lang.Thread.run(Thread.java:662)
> >
> >
> >
> > On Tue, Sep 25, 2012 at 3:43 PM, Romain Manni-Bucau
> > <rm...@gmail.com>wrote:
> >
> > > can you share the sample please?
> > >
> > > *Romain Manni-Bucau*
> > > *Twitter: @rmannibucau*
> > > *Blog: **http://rmannibucau.wordpress.com/*<
> > > http://rmannibucau.wordpress.com/>
> > > *LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
> > >
> > >
> > >
> > >
> > > 2012/9/25 Luca Merolla <lu...@gmail.com>
> > >
> > > > Hi everyone,
> > > >
> > > > I have noticed this blog entry
> > > >
> > > >
> > >
> >
> http://rmannibucau.wordpress.com/2012/05/15/tomee-or-how-to-use-websocket-with-cdi/
> > > > and
> > > > I have compared to my solution which is very similar but on my case
> > since
> > > > CDI was not working I had to put some code in the init() method of my
> > > > servlet to retrieve the Beans, like this:
> > > >
> > > > jsonProvider = (JsonProviderBean) getServletContext().getAttribute(
> > > > GalaxyConstants.JSON_PROVIDER_BEAN);
> > > > processor = WebSocketConnectionProcessor.getInstance(jsonProvider);
> > > >
> > > > the jsonProvider was created in my ServletContextListener class and
> the
> > > > servlet was simply retrieving it. For the
> WebSocketConnectionProcessor
> > I
> > > > opted to make the class singleton and in this way it was working.
> > > >
> > > > Despite this solution, I have tried to follow the example from the
> blog
> > > of
> > > > Romain because I would like to inject the processor.
> > > >
> > > > @WebServlet(urlPatterns = "/gametable", asyncSupported = true,
> > > > loadOnStartup = 1)
> > > > public class WebSocketGametableServlet extends WebSocketServlet
> > > implements
> > > > IWebSocketServlet {
> > > >
> > > > private static final long serialVersionUID = -6573757123424131706L;
> > > >
> > > > protected final Logger LOGGER = LoggerFactory.getLogger(getClass()
> > > > .getName());
> > > >
> > > > @Inject
> > > > private WebSocketGametableProcessor processor;
> > > > private JsonProviderBean jsonProvider;
> > > >
> > > > public void init() throws ServletException {
> > > > LOGGER.info("Initialize Lobby WebSocket servlet");
> > > > super.init();
> > > >
> > > > initializeWebSocketProcessor();
> > > > LOGGER.info("Web Socket connection processor initialized");
> > > > }
> > > >
> > > > /**
> > > >  * Get the JSON provider bean from the servlet context and retrieve
> the
> > > >  * {@link WebSocketGametableProcessor} instance
> > > >  */
> > > > private void initializeWebSocketProcessor() {
> > > > LOGGER.info("Getting the json provider bean");
> > > > jsonProvider = (JsonProviderBean) getServletContext().getAttribute(
> > > > GalaxyConstants.JSON_PROVIDER_BEAN);
> > > >
> > > > // LOGGER.info("Getting the websocker processor instance");
> > > > // processor = WebSocketGametableProcessor.getInstance(jsonProvider);
> > > > }
> > > >
> > > > /*
> > > >  * (non-Javadoc)
> > > >  *
> > > >  * @see
> > > >  *
> > org.apache.catalina.websocket.WebSocketServlet#createWebSocketInbound
> > > >  * (java.lang.String, javax.servlet.http.HttpServletRequest)
> > > >  */
> > > > @Override
> > > > protected StreamInbound createWebSocketInbound(String arg0,
> > > > HttpServletRequest request) {
> > > > return new GametableMessageInbound(this,
> > > > (Long) request.getAttribute(WebSocketConstants.SESSION_ID));
> > > > }
> > > >
> > > > @Override
> > > > public void addConnection(LobbyKey lobbyKey,
> > > > AbstractMessageInbound lobbyConnection) {
> > > > if (processor == null)
> > > > LOGGER.info("add connection...FAIL");
> > > > else
> > > > LOGGER.info("add connection...");
> > > > }
> > > >
> > > > @Override
> > > > public void removeConnection(LobbyKey lobbyKey,
> > > > AbstractMessageInbound lobbyConnection) {
> > > > if (processor == null)
> > > > LOGGER.info("remove connection...FAIL");
> > > > else
> > > > LOGGER.info("remove connection...");
> > > > }
> > > >
> > > > @Override
> > > > public void processTextCommand(String jsonCommand) {
> > > > processor.processWebsocketCommand(jsonCommand);
> > > > }
> > > > }
> > > >
> > > > The WebSocketGametableProcessor has no other dependencies
> > > >
> > > > public class WebSocketGametableProcessor {
> > > >
> > > > private static final Logger LOGGER = LoggerFactory
> > > > .getLogger(WebSocketGametableProcessor.class);
> > > >
> > > > private WebSocketGametableProcessor() {
> > > >
> > > > }
> > > >
> > > > public void processWebsocketCommand(String jsonCommand) {
> > > > LOGGER.info("Received json command |{}|", jsonCommand);
> > > > }
> > > > }
> > > >
> > > > And as result I have that the WebSocketGametableProcessor is null in
> > the
> > > > servlet. I was using the snapshot from last week, I have just run the
> > > > latest 1.5.1-SNAPSHOT and the issue persist.
> > > >
> > > > Luca
> > > >
> > >
> >
>

Re: CDI Injection in WebSocket is not working

Posted by Romain Manni-Bucau <rm...@gmail.com>.
hmm,

can you test in a simple war please (== not an ear)?

*Romain Manni-Bucau*
*Twitter: @rmannibucau*
*Blog: **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
*LinkedIn: **http://fr.linkedin.com/in/rmannibucau*




2012/9/25 Luca Merolla <lu...@gmail.com>

> Here is the sample:
>
> *
>
> http://www.fileconvoy.com/dfl.php?id=g9c64757621dc468115011126f019deb361277a
> *
> *
> *
> Deployed under apps/Test.ear it should give the following errors:
>
> On Deployment I get this exception: which however I have already noticed in
> other web app, but it doesn't seems to effect the CDI injection later when
> the app is running. In the other web app I get similar exception but it's
> only on deployment and then the @Inject works.
>
> http://pastebin.com/qvqn3WsD
>
> After deployment, If I try to establish the websocket connection and I get:
>
> Sep 25, 2012 4:36:42 PM
> org.pokernuvola.galaxy.gametable.WebSocketGametableServlet
> createWebSocketInbound
> INFO: CREATE connection...FAIL
> Sep 25, 2012 4:36:42 PM
> org.pokernuvola.galaxy.gametable.GametableMessageInbound onOpen
> INFO: Opening the connection |1|
> Sep 25, 2012 4:36:42 PM
> org.pokernuvola.galaxy.gametable.WebSocketGametableServlet
> addGametableConnection
> INFO: add connection...FAIL
> Sep 25, 2012 4:36:52 PM
> org.pokernuvola.galaxy.gametable.GametableMessageInbound onTextMessage
> INFO: Got a message text! |Ping|
> Sep 25, 2012 4:36:52 PM
> org.apache.coyote.AbstractProtocol$AbstractConnectionHandler process
> SEVERE: Error reading request, ignored
> java.lang.NullPointerException
>         at
>
> org.pokernuvola.galaxy.gametable.WebSocketGametableServlet.processTextCommand(WebSocketGametableServlet.java:74)
>         at
>
> org.pokernuvola.galaxy.gametable.GametableMessageInbound.onTextMessage(GametableMessageInbound.java:35)
>         at
>
> org.apache.catalina.websocket.MessageInbound.onTextData(MessageInbound.java:74)
>         at
>
> org.apache.catalina.websocket.StreamInbound.doOnTextData(StreamInbound.java:186)
>         at
> org.apache.catalina.websocket.StreamInbound.onData(StreamInbound.java:134)
>         at
>
> org.apache.coyote.http11.upgrade.UpgradeProcessor.upgradeDispatch(UpgradeProcessor.java:83)
>         at
>
> org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:583)
>         at
>
> org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
>         at
>
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
>         at
>
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
>         at java.lang.Thread.run(Thread.java:662)
>
>
>
> On Tue, Sep 25, 2012 at 3:43 PM, Romain Manni-Bucau
> <rm...@gmail.com>wrote:
>
> > can you share the sample please?
> >
> > *Romain Manni-Bucau*
> > *Twitter: @rmannibucau*
> > *Blog: **http://rmannibucau.wordpress.com/*<
> > http://rmannibucau.wordpress.com/>
> > *LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
> >
> >
> >
> >
> > 2012/9/25 Luca Merolla <lu...@gmail.com>
> >
> > > Hi everyone,
> > >
> > > I have noticed this blog entry
> > >
> > >
> >
> http://rmannibucau.wordpress.com/2012/05/15/tomee-or-how-to-use-websocket-with-cdi/
> > > and
> > > I have compared to my solution which is very similar but on my case
> since
> > > CDI was not working I had to put some code in the init() method of my
> > > servlet to retrieve the Beans, like this:
> > >
> > > jsonProvider = (JsonProviderBean) getServletContext().getAttribute(
> > > GalaxyConstants.JSON_PROVIDER_BEAN);
> > > processor = WebSocketConnectionProcessor.getInstance(jsonProvider);
> > >
> > > the jsonProvider was created in my ServletContextListener class and the
> > > servlet was simply retrieving it. For the WebSocketConnectionProcessor
> I
> > > opted to make the class singleton and in this way it was working.
> > >
> > > Despite this solution, I have tried to follow the example from the blog
> > of
> > > Romain because I would like to inject the processor.
> > >
> > > @WebServlet(urlPatterns = "/gametable", asyncSupported = true,
> > > loadOnStartup = 1)
> > > public class WebSocketGametableServlet extends WebSocketServlet
> > implements
> > > IWebSocketServlet {
> > >
> > > private static final long serialVersionUID = -6573757123424131706L;
> > >
> > > protected final Logger LOGGER = LoggerFactory.getLogger(getClass()
> > > .getName());
> > >
> > > @Inject
> > > private WebSocketGametableProcessor processor;
> > > private JsonProviderBean jsonProvider;
> > >
> > > public void init() throws ServletException {
> > > LOGGER.info("Initialize Lobby WebSocket servlet");
> > > super.init();
> > >
> > > initializeWebSocketProcessor();
> > > LOGGER.info("Web Socket connection processor initialized");
> > > }
> > >
> > > /**
> > >  * Get the JSON provider bean from the servlet context and retrieve the
> > >  * {@link WebSocketGametableProcessor} instance
> > >  */
> > > private void initializeWebSocketProcessor() {
> > > LOGGER.info("Getting the json provider bean");
> > > jsonProvider = (JsonProviderBean) getServletContext().getAttribute(
> > > GalaxyConstants.JSON_PROVIDER_BEAN);
> > >
> > > // LOGGER.info("Getting the websocker processor instance");
> > > // processor = WebSocketGametableProcessor.getInstance(jsonProvider);
> > > }
> > >
> > > /*
> > >  * (non-Javadoc)
> > >  *
> > >  * @see
> > >  *
> org.apache.catalina.websocket.WebSocketServlet#createWebSocketInbound
> > >  * (java.lang.String, javax.servlet.http.HttpServletRequest)
> > >  */
> > > @Override
> > > protected StreamInbound createWebSocketInbound(String arg0,
> > > HttpServletRequest request) {
> > > return new GametableMessageInbound(this,
> > > (Long) request.getAttribute(WebSocketConstants.SESSION_ID));
> > > }
> > >
> > > @Override
> > > public void addConnection(LobbyKey lobbyKey,
> > > AbstractMessageInbound lobbyConnection) {
> > > if (processor == null)
> > > LOGGER.info("add connection...FAIL");
> > > else
> > > LOGGER.info("add connection...");
> > > }
> > >
> > > @Override
> > > public void removeConnection(LobbyKey lobbyKey,
> > > AbstractMessageInbound lobbyConnection) {
> > > if (processor == null)
> > > LOGGER.info("remove connection...FAIL");
> > > else
> > > LOGGER.info("remove connection...");
> > > }
> > >
> > > @Override
> > > public void processTextCommand(String jsonCommand) {
> > > processor.processWebsocketCommand(jsonCommand);
> > > }
> > > }
> > >
> > > The WebSocketGametableProcessor has no other dependencies
> > >
> > > public class WebSocketGametableProcessor {
> > >
> > > private static final Logger LOGGER = LoggerFactory
> > > .getLogger(WebSocketGametableProcessor.class);
> > >
> > > private WebSocketGametableProcessor() {
> > >
> > > }
> > >
> > > public void processWebsocketCommand(String jsonCommand) {
> > > LOGGER.info("Received json command |{}|", jsonCommand);
> > > }
> > > }
> > >
> > > And as result I have that the WebSocketGametableProcessor is null in
> the
> > > servlet. I was using the snapshot from last week, I have just run the
> > > latest 1.5.1-SNAPSHOT and the issue persist.
> > >
> > > Luca
> > >
> >
>

Re: CDI Injection in WebSocket is not working

Posted by Luca Merolla <lu...@gmail.com>.
Here is the sample:

*
http://www.fileconvoy.com/dfl.php?id=g9c64757621dc468115011126f019deb361277a
*
*
*
Deployed under apps/Test.ear it should give the following errors:

On Deployment I get this exception: which however I have already noticed in
other web app, but it doesn't seems to effect the CDI injection later when
the app is running. In the other web app I get similar exception but it's
only on deployment and then the @Inject works.

http://pastebin.com/qvqn3WsD

After deployment, If I try to establish the websocket connection and I get:

Sep 25, 2012 4:36:42 PM
org.pokernuvola.galaxy.gametable.WebSocketGametableServlet
createWebSocketInbound
INFO: CREATE connection...FAIL
Sep 25, 2012 4:36:42 PM
org.pokernuvola.galaxy.gametable.GametableMessageInbound onOpen
INFO: Opening the connection |1|
Sep 25, 2012 4:36:42 PM
org.pokernuvola.galaxy.gametable.WebSocketGametableServlet
addGametableConnection
INFO: add connection...FAIL
Sep 25, 2012 4:36:52 PM
org.pokernuvola.galaxy.gametable.GametableMessageInbound onTextMessage
INFO: Got a message text! |Ping|
Sep 25, 2012 4:36:52 PM
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler process
SEVERE: Error reading request, ignored
java.lang.NullPointerException
        at
org.pokernuvola.galaxy.gametable.WebSocketGametableServlet.processTextCommand(WebSocketGametableServlet.java:74)
        at
org.pokernuvola.galaxy.gametable.GametableMessageInbound.onTextMessage(GametableMessageInbound.java:35)
        at
org.apache.catalina.websocket.MessageInbound.onTextData(MessageInbound.java:74)
        at
org.apache.catalina.websocket.StreamInbound.doOnTextData(StreamInbound.java:186)
        at
org.apache.catalina.websocket.StreamInbound.onData(StreamInbound.java:134)
        at
org.apache.coyote.http11.upgrade.UpgradeProcessor.upgradeDispatch(UpgradeProcessor.java:83)
        at
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:583)
        at
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
        at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:662)



On Tue, Sep 25, 2012 at 3:43 PM, Romain Manni-Bucau
<rm...@gmail.com>wrote:

> can you share the sample please?
>
> *Romain Manni-Bucau*
> *Twitter: @rmannibucau*
> *Blog: **http://rmannibucau.wordpress.com/*<
> http://rmannibucau.wordpress.com/>
> *LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
>
>
>
>
> 2012/9/25 Luca Merolla <lu...@gmail.com>
>
> > Hi everyone,
> >
> > I have noticed this blog entry
> >
> >
> http://rmannibucau.wordpress.com/2012/05/15/tomee-or-how-to-use-websocket-with-cdi/
> > and
> > I have compared to my solution which is very similar but on my case since
> > CDI was not working I had to put some code in the init() method of my
> > servlet to retrieve the Beans, like this:
> >
> > jsonProvider = (JsonProviderBean) getServletContext().getAttribute(
> > GalaxyConstants.JSON_PROVIDER_BEAN);
> > processor = WebSocketConnectionProcessor.getInstance(jsonProvider);
> >
> > the jsonProvider was created in my ServletContextListener class and the
> > servlet was simply retrieving it. For the WebSocketConnectionProcessor I
> > opted to make the class singleton and in this way it was working.
> >
> > Despite this solution, I have tried to follow the example from the blog
> of
> > Romain because I would like to inject the processor.
> >
> > @WebServlet(urlPatterns = "/gametable", asyncSupported = true,
> > loadOnStartup = 1)
> > public class WebSocketGametableServlet extends WebSocketServlet
> implements
> > IWebSocketServlet {
> >
> > private static final long serialVersionUID = -6573757123424131706L;
> >
> > protected final Logger LOGGER = LoggerFactory.getLogger(getClass()
> > .getName());
> >
> > @Inject
> > private WebSocketGametableProcessor processor;
> > private JsonProviderBean jsonProvider;
> >
> > public void init() throws ServletException {
> > LOGGER.info("Initialize Lobby WebSocket servlet");
> > super.init();
> >
> > initializeWebSocketProcessor();
> > LOGGER.info("Web Socket connection processor initialized");
> > }
> >
> > /**
> >  * Get the JSON provider bean from the servlet context and retrieve the
> >  * {@link WebSocketGametableProcessor} instance
> >  */
> > private void initializeWebSocketProcessor() {
> > LOGGER.info("Getting the json provider bean");
> > jsonProvider = (JsonProviderBean) getServletContext().getAttribute(
> > GalaxyConstants.JSON_PROVIDER_BEAN);
> >
> > // LOGGER.info("Getting the websocker processor instance");
> > // processor = WebSocketGametableProcessor.getInstance(jsonProvider);
> > }
> >
> > /*
> >  * (non-Javadoc)
> >  *
> >  * @see
> >  * org.apache.catalina.websocket.WebSocketServlet#createWebSocketInbound
> >  * (java.lang.String, javax.servlet.http.HttpServletRequest)
> >  */
> > @Override
> > protected StreamInbound createWebSocketInbound(String arg0,
> > HttpServletRequest request) {
> > return new GametableMessageInbound(this,
> > (Long) request.getAttribute(WebSocketConstants.SESSION_ID));
> > }
> >
> > @Override
> > public void addConnection(LobbyKey lobbyKey,
> > AbstractMessageInbound lobbyConnection) {
> > if (processor == null)
> > LOGGER.info("add connection...FAIL");
> > else
> > LOGGER.info("add connection...");
> > }
> >
> > @Override
> > public void removeConnection(LobbyKey lobbyKey,
> > AbstractMessageInbound lobbyConnection) {
> > if (processor == null)
> > LOGGER.info("remove connection...FAIL");
> > else
> > LOGGER.info("remove connection...");
> > }
> >
> > @Override
> > public void processTextCommand(String jsonCommand) {
> > processor.processWebsocketCommand(jsonCommand);
> > }
> > }
> >
> > The WebSocketGametableProcessor has no other dependencies
> >
> > public class WebSocketGametableProcessor {
> >
> > private static final Logger LOGGER = LoggerFactory
> > .getLogger(WebSocketGametableProcessor.class);
> >
> > private WebSocketGametableProcessor() {
> >
> > }
> >
> > public void processWebsocketCommand(String jsonCommand) {
> > LOGGER.info("Received json command |{}|", jsonCommand);
> > }
> > }
> >
> > And as result I have that the WebSocketGametableProcessor is null in the
> > servlet. I was using the snapshot from last week, I have just run the
> > latest 1.5.1-SNAPSHOT and the issue persist.
> >
> > Luca
> >
>

Re: CDI Injection in WebSocket is not working

Posted by Romain Manni-Bucau <rm...@gmail.com>.
can you share the sample please?

*Romain Manni-Bucau*
*Twitter: @rmannibucau*
*Blog: **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
*LinkedIn: **http://fr.linkedin.com/in/rmannibucau*




2012/9/25 Luca Merolla <lu...@gmail.com>

> Hi everyone,
>
> I have noticed this blog entry
>
> http://rmannibucau.wordpress.com/2012/05/15/tomee-or-how-to-use-websocket-with-cdi/
> and
> I have compared to my solution which is very similar but on my case since
> CDI was not working I had to put some code in the init() method of my
> servlet to retrieve the Beans, like this:
>
> jsonProvider = (JsonProviderBean) getServletContext().getAttribute(
> GalaxyConstants.JSON_PROVIDER_BEAN);
> processor = WebSocketConnectionProcessor.getInstance(jsonProvider);
>
> the jsonProvider was created in my ServletContextListener class and the
> servlet was simply retrieving it. For the WebSocketConnectionProcessor I
> opted to make the class singleton and in this way it was working.
>
> Despite this solution, I have tried to follow the example from the blog of
> Romain because I would like to inject the processor.
>
> @WebServlet(urlPatterns = "/gametable", asyncSupported = true,
> loadOnStartup = 1)
> public class WebSocketGametableServlet extends WebSocketServlet implements
> IWebSocketServlet {
>
> private static final long serialVersionUID = -6573757123424131706L;
>
> protected final Logger LOGGER = LoggerFactory.getLogger(getClass()
> .getName());
>
> @Inject
> private WebSocketGametableProcessor processor;
> private JsonProviderBean jsonProvider;
>
> public void init() throws ServletException {
> LOGGER.info("Initialize Lobby WebSocket servlet");
> super.init();
>
> initializeWebSocketProcessor();
> LOGGER.info("Web Socket connection processor initialized");
> }
>
> /**
>  * Get the JSON provider bean from the servlet context and retrieve the
>  * {@link WebSocketGametableProcessor} instance
>  */
> private void initializeWebSocketProcessor() {
> LOGGER.info("Getting the json provider bean");
> jsonProvider = (JsonProviderBean) getServletContext().getAttribute(
> GalaxyConstants.JSON_PROVIDER_BEAN);
>
> // LOGGER.info("Getting the websocker processor instance");
> // processor = WebSocketGametableProcessor.getInstance(jsonProvider);
> }
>
> /*
>  * (non-Javadoc)
>  *
>  * @see
>  * org.apache.catalina.websocket.WebSocketServlet#createWebSocketInbound
>  * (java.lang.String, javax.servlet.http.HttpServletRequest)
>  */
> @Override
> protected StreamInbound createWebSocketInbound(String arg0,
> HttpServletRequest request) {
> return new GametableMessageInbound(this,
> (Long) request.getAttribute(WebSocketConstants.SESSION_ID));
> }
>
> @Override
> public void addConnection(LobbyKey lobbyKey,
> AbstractMessageInbound lobbyConnection) {
> if (processor == null)
> LOGGER.info("add connection...FAIL");
> else
> LOGGER.info("add connection...");
> }
>
> @Override
> public void removeConnection(LobbyKey lobbyKey,
> AbstractMessageInbound lobbyConnection) {
> if (processor == null)
> LOGGER.info("remove connection...FAIL");
> else
> LOGGER.info("remove connection...");
> }
>
> @Override
> public void processTextCommand(String jsonCommand) {
> processor.processWebsocketCommand(jsonCommand);
> }
> }
>
> The WebSocketGametableProcessor has no other dependencies
>
> public class WebSocketGametableProcessor {
>
> private static final Logger LOGGER = LoggerFactory
> .getLogger(WebSocketGametableProcessor.class);
>
> private WebSocketGametableProcessor() {
>
> }
>
> public void processWebsocketCommand(String jsonCommand) {
> LOGGER.info("Received json command |{}|", jsonCommand);
> }
> }
>
> And as result I have that the WebSocketGametableProcessor is null in the
> servlet. I was using the snapshot from last week, I have just run the
> latest 1.5.1-SNAPSHOT and the issue persist.
>
> Luca
>