Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get AtmosphereFramework from servlet context on JBoss EAP 6.1 #1716

Closed
master-den opened this issue Sep 16, 2014 · 20 comments
Closed

Can't get AtmosphereFramework from servlet context on JBoss EAP 6.1 #1716

master-den opened this issue Sep 16, 2014 · 20 comments

Comments

@master-den
Copy link

Hi!

Version Atmosphere is 2.2.1 - 2.3.0-snapshot, JDK 7, JBoss EAP 6.1, Windows Server 2008R2.

I need getting BroadcasterFactory. I’m getting it as described in p.Servlet in
https://github.com/Atmosphere/atmosphere/wiki/Getting-BroadcasterFactory-and-AtmosphereResourceFactory-with-2.2-and-newer

But, on JBoss EAP 6.1 (BIO connector) in servlet context not contained AtmosphereFramework.
The AtmosphereServlet configured as described in article by link above for getting it from servlet context.

The ServletContext under JBoss EAP 6.1 contains:
2014-09-16 00:49:20,084 INFO (http-/127.0.0.1:8080-5) Attribute=org.apache.catalina.SERVLET_VERSION, value=3.0
2014-09-16 00:49:20,084 INFO (http-/127.0.0.1:8080-5) Attribute=org.springframework.web.context.support.ServletContextScope, value=org.springframework.web.context.support.ServletContextScope@faedca
2014-09-16 00:49:20,084 INFO (http-/127.0.0.1:8080-5) Attribute=org.apache.catalina.WELCOME_FILES, value=[index.html]
2014-09-16 00:49:20,084 INFO (http-/127.0.0.1:8080-5) Attribute=javax.servlet.context.tempdir ...
2014-09-16 00:49:20,084 INFO (http-/127.0.0.1:8080-5) Attribute=org.atmosphere.cpr.BroadcasterFactory, value=org.atmosphere.cpr.DefaultBroadcasterFactory@108d6ac
2014-09-16 00:49:20,084 INFO (http-/127.0.0.1:8080-5) Attribute=org.apache.catalina.resources, value=org.apache.naming.resources.ProxyDirContext@145ef27
2014-09-16 00:49:20,084 INFO (http-/127.0.0.1:8080-5) Attribute=org.springframework.web.context.WebApplicationContext.ROOT, value=Root WebApplicationContext: startup date [Tue Sep 16 00:48:30 MSK 2014]; root of context hierarchy
2014-09-16 00:49:20,084 INFO (http-/127.0.0.1:8080-5) Attribute=org.jboss.as.jsf.FACES_ANNOTATIONS, value={}
2014-09-16 00:49:20,084 INFO (http-/127.0.0.1:8080-5) Attribute=org.apache.catalina.JSP_PROPERTY_GROUPS, value={}
2014-09-16 00:49:20,084 INFO (http-/127.0.0.1:8080-5) Attribute=org.atmosphere.cpr.ANNOTATION_MAP ...
2014-09-16 00:49:20,084 INFO (http-/127.0.0.1:8080-5) Attribute=com.sun.faces.patternCache, value={ = }
2014-09-16 00:49:20,084 INFO (http-/127.0.0.1:8080-5) Attribute=com.sun.faces.InitFacesContext, value=com.sun.faces.config.InitFacesContext@f2c8c0
2014-09-16 00:49:20,084 INFO (http-/127.0.0.1:8080-5) Attribute=org.apache.catalina.JSP_TAG_LIBRARIES ...
2014-09-16 00:49:20,084 INFO (http-/127.0.0.1:8080-5) Attribute=org.springframework.web.servlet.FrameworkServlet.CONTEXT.websrvCallAdmin ...
2014-09-16 00:49:20,084 INFO (http-/127.0.0.1:8080-5) Attribute=org.apache.tomcat.InstanceManager, value=org.jboss.as.web.deployment.WebInjectionContainer@401520

However, in servlet context I found a broadcaster factory, which should be getting from atmosphere framework. In result, I'm getting BroadcasterFactory directly from servlet context.

@jfarcand
Copy link
Member

So you do you need exactly? We need to improve the documentation?

@master-den
Copy link
Author

In future, for properly interaction with atmosphere, I need getting AtmosphereFramework from servlet context as described in document.

@jfarcand
Copy link
Member

JBoss EAP is using an old version of Servlet (2.5) I suspect. Can you confirm? That's the issue

@master-den
Copy link
Author

As described in p.1.2.1.1 official documentation (https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/6.1/pdf/Development_Guide/JBoss_Enterprise_Application_Platform-6.1-Development_Guide-en-US.pdf):
"JBoss EAP 6 is a certified implementation of the Java Enterprise Edition 6 Full Profile and Web Profile specifications."
The web profile requirements are also supports Servlet 3.0 (p.1.2.1.2 in document above).

The my web.xml specified as standard for Servlet 3.0:

<web-app 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/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true"
  id="WSCallAdmin">
...
</web-app>

My atmosphere app is work correctly, except only impossibility getting AtmosphereFramework from servlet context.

@jfarcand
Copy link
Member

OK, but how do you try to access it? Are you starting Atmosphere the way it is described in the document?

@master-den
Copy link
Author

In web.xml:

    <servlet>
        <servlet-name>AtmosphereServlet</servlet-name>
        <servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>org.atmosphere.useNative</param-name>
            <!-- false is default. In 2.2.x this property is ignored. The native mode requires some
               additional libs (atmosphere-runtime-native.jar, atmosphere-compat-xxx.jar) and settings
               for JBoss (Tomcat) container. See:
               https://github.com/Atmosphere/atmosphere/wiki/Installing-AtmosphereServlet-with-or-without-native-support -->
            <param-value>false</param-value>
        </init-param>
        <!-- For JBoss: ClasspathResourceConfig  -->
        <init-param>
            <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
            <param-value>com.sun.jersey.api.core.ClasspathResourceConfig</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.property.classpath</param-name>
            <param-value>/WEB-INF/classes/ru/mda/calladmin/controller</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>

in Java code:

  import org.atmosphere.util.ServletContextFactory;
...  
  final ServletContext ctx = ServletContextFactory.getDefault().getServletContext();
...
  AtmosphereFramework framework = (AtmosphereFramework) ctx.getAttribute("AtmosphereServlet");
  BroadcasterFactory broadcasterFactory = framework.getBroadcasterFactory(); // NPE!
...

@master-den
Copy link
Author

Which document to read?
In https://github.com/Atmosphere/atmosphere/wiki/Getting-BroadcasterFactory-and-AtmosphereResourceFactory-with-2.2-and-newer has writen:

AtmosphereFramework is also stored as an application scoped attribute under the name of AtmosphereServlet's servlet name. This way is useful when using web.xml.

<servlet>
    <servlet-name>AtmosphereServlet</servlet-name>
    <servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
    <async-supported>true</async-supported>
</servlet>
AtmosphereFramework framework = (AtmosphereFramework) servletContext.getAttribute("AtmosphereServlet");
BroadcasterFactory broadcasterFactory = framework.getBroadcasterFactory();
AtmosphereResourceFactory atmosphereResourceFactory = framework.atmosphereFactory();

On pure Tomcat 7 the code from above is worked. On JBoss EAP 6.1 is not.
What I don't understand?

@master-den
Copy link
Author

@jfarcand
Copy link
Member

Ok fair enough. It seems to be a bug with JBoss. But don't use the ServletContextFactory and instead retrieve it from the AtmosphereConfig.

@jfarcand jfarcand reopened this Sep 16, 2014
@master-den
Copy link
Author

FYI.
The @Inject an AtmosphereFramework on JBoss EAP is thrown error:

...
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [AtmosphereFramework] with qualifiers [@Default] at injection point [[field] @Inject private ru.service.AtmosphereFrameworkSupplier.atmosphereFramework]
    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:311)
    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:280)

@jfarcand
Copy link
Member

@master-den Please open another issue for the WELD. Make sure you are using 2.3.0-SNAPSHOT

@jjfraney
Copy link

A @Producer works in eap 6.3. I expect it would work in eap 6.1 as well.

Does it have to be more complicated?

package in.your.package;
// don't forget the beans.xml file!

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.servlet.ServletContext;

import org.atmosphere.cpr.AtmosphereFramework;
import org.atmosphere.cpr.BroadcasterFactory;
import org.atmosphere.util.ServletContextFactory;

@ApplicationScoped
public class Producer {
    // TODO: cache the framework in a static?

    @Produces
    public BroadcasterFactory getBroadcastFactory() {

        // TODO: replace deprecated getServletContext()....with what?
        ServletContext servletContext = ServletContextFactory.getDefault().getServletContext();
        AtmosphereFramework framework = (AtmosphereFramework) servletContext.getAttribute("AtmosphereServlet");
        BroadcasterFactory broadcasterFactory = framework.getBroadcasterFactory();
        return broadcasterFactory;

    }

}

@jfarcand
Copy link
Member

@jjfraney Not following you here. What do you means?

@jjfraney
Copy link

Ok, I googled how to cdi @Inject a BrodcasterFactory and came here. Read the discussion about how it does not work due to bug in Weld. Find it hard to believe (Weld is reference implementation of CDI). Anyway, I could not find a sample in any other google result. I only want to cut-n-paste so not to spend too much time, but alas. So, I played around. I took the time to figure out how to build a BroadcasterFactory and wrote it as a cdi @Producer. Now, I can @Inject BroadcasterFactory. I thought others would like to know. I don't think an issue raised in jboss against Weld should block someone from injecting a BroadcasterFactory.

@jjfraney
Copy link

Yes. Its not the same.

I have a separate rest service. Its not an atmosphere @managedservice. It is just a jax-rs compliant rest service. When a POST method arrives, I want to broadcast, and want to @Inject BroadcasterFactory. This pages seems to imply it just works, but it didn't: https://github.com/Atmosphere/atmosphere/wiki/Getting-BroadcasterFactory-and-AtmosphereResourceFactory-with-2.2-and-newer

Then, I found the sample you refer to. I added atmosphere-cdi to the project. I got an 'AbstractMethodException' due to a method missing from CDIObjectFactory. Obviously, I got versioning screwed up, but didn't have patience to resolve.

I looked at the Object Factory code and tried to imagine how it could work. I'm not an expert, but I could not find a hook in CDI implementation such that the Factory would be used to resolve beans. I think maybe the cdi bean resolver runs at deploy time and maybe the Factory is not available then. Also, the lack of a beans.xml file in the atmosphere jar kind of drops my confidence that it can work: In CDI, the Factory pattern is implemented with @Producer and without a beans.xml file, there exists NO operative @producers in the atmosphere-cdi jar.

Further, if the solution is provided by one of my @Producer, then, I think its simpler than using the atmosphere provided CDIObjectFactory, atmosphere-cdi and then trying to get it all to work.

Is there some disadvantage to using a @Producer in my app? Is there a fault in the code above?

@jfarcand
Copy link
Member

jfarcand commented Feb 3, 2015

@jjfraney There is a bean.xml in that sample. I've tested with Wildfly 8.2.0.Final and everything works as expected with CDI

12:02:31,773 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-2) JBAS017534: Registered web context: /atmosphere-chat-cdi
12:02:31,802 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 28) JBAS018559: Deployed "atmosphere-chat-cdi.war" (runtime-name : "atmosphere-chat-cdi.war")
12:02:32,053 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9990/management
12:02:32,054 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990
12:02:32,054 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015874: WildFly 8.2.0.Final "Tweek" started in 4905ms - Started 282 of 336 services (92 services are lazy, passive or on-demand)
12:02:32,174 INFO  [org.atmosphere.cpr.AtmosphereFramework] (Thread-81) Latest version of Atmosphere's JavaScript Client 2.2.6
12:02:32,174 INFO  [org.atmosphere.cpr.AtmosphereFramework] (Thread-81)

    Current version of Atmosphere 2.3.0-SNAPSHOT
    Newest version of Atmosphere available 2.2.4


12:02:53,112 INFO  [org.atmosphere.samples.chat.CDIChat] (default task-14) Browser 15c6aba9-ce06-4098-92e3-ee1923f2c17e connected.
12:02:53,113 INFO  [org.atmosphere.samples.chat.CDIChat] (default task-14) Injected Factory org.atmosphere.cpr.DefaultBroadcasterFactory connected.
12:04:53,116 INFO  [org.atmosphere.samples.chat.CDIChat] (default task-15) Browser 15c6aba9-ce06-4098-92e3-ee1923f2c17e closed the connection
12:04:53,139 INFO  [org.atmosphere.samples.chat.CDIChat] (default task-16) null just send is inactive and closed the connection. Will reconnect in 5000
12:04:58,117 INFO  [org.atmosphere.samples.chat.CDIChat] (default task-17) Browser 15c6aba9-ce06-4098-92e3-ee1923f2c17e connected.
12:04:58,117 INFO  [org.atmosphere.samples.chat.CDIChat] (default task-17) Injected Factory org.atmosphere.cpr.DefaultBroadcasterFactory connected.
12:05:57,511 INFO  [org.atmosphere.samples.chat.CDIChat] (default task-18) dasds just send dasds
12:05:57,985 INFO  [org.atmosphere.samples.chat.CDIChat] (default task-19) dasds just send dsa

@rooseveltlai
Copy link

Can someone provide a solution to the original problem: how to get AtmosphereFramework in 2.2.x without using servlet context and AtmosphereResource?
I've had the same problem on Weblogic 12c. Below code returns null:

    ServletContext servletContext = ServletContextFactory.getDefault().getServletContext();
    AtmosphereFramework framework = servletContext.getAttribute("AtmosphereServlet");

@jfarcand
Copy link
Member

@rooseveltlai what is the servlet-name of the AtmosphereServlet in web.xml?

@NtdStart
Copy link

I also encountered a similar problem. I added beans.xml but the problem remains unresolved. I use Red Hat JBoss Enterprise Application Platform - Version 6.3.0.GA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants