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 <[email protected]>

> 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
>

Reply via email to