WELD with embedded Jetty
erg144 Jan 7, 2016 7:26 AMI've been trying to figure out how to run weld with an embedded version of jetty and not having much luck (in fact, i can not seem to get it to run under jetty at all). I seem to get a host of strange errors that I can not seem to resolve.
When running under jetty I will get this following error:
java.lang.RuntimeException: WELD-ENV-001024: Could not bind BeanManager reference to JNDI: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial|If the naming context is read-only, you may need to use a configuration to bind the BeanManager instead, such as Tomcats context.xml or Jettys jetty-web.xml.
this would seem I don't have a web.xml and/or a jetty-env.xml - both which exist and seem proper.
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<resource-env-ref>
<description>Object factory for the CDI Bean Manager</description>
<resource-env-ref-name>BeanManager</resource-env-ref-name>
<resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
</resource-env-ref>
</web-app>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"
"http://www.eclipse.org/jetty/configure.dtd">
<Configure id="webAppCtx" class="org.eclipse.jetty.webapp.WebAppContext">
<New id="BeanManager" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>
<Ref id="webAppCtx" />
</Arg>
<Arg>BeanManager</Arg>
<Arg>
<New class="javax.naming.Reference">
<Arg>javax.enterprise.inject.spi.BeanManager</Arg>
<Arg>org.jboss.weld.resources.ManagerObjectFactory</Arg>
<Arg />
</New>
</Arg>
</New>
</Configure>
When I try and run embedded I get the following:
INFO: WELD-ENV-001007: Initialize Weld using ServletContextListener
Jan 07, 2016 7:20:01 AM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 2.3.1 (Final)
Jan 07, 2016 7:20:01 AM org.jboss.weld.bootstrap.WeldStartup startContainer
INFO: WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
Jan 07, 2016 7:20:01 AM org.jboss.weld.interceptor.util.InterceptionTypeRegistry <clinit>
WARN: WELD-001700: Interceptor annotation class javax.ejb.PostActivate not found, interception based on it is not enabled
Jan 07, 2016 7:20:01 AM org.jboss.weld.interceptor.util.InterceptionTypeRegistry <clinit>
WARN: WELD-001700: Interceptor annotation class javax.ejb.PrePassivate not found, interception based on it is not enabled
Jan 07, 2016 7:20:01 AM org.jboss.weld.environment.jetty.JettyContainer initialize
INFO: WELD-ENV-001201: Jetty 7.2+ detected, CDI injection will be available in Servlets and Filters. Injection into Listeners is not supported.
2016-01-07 07:20:03.520:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@3ebff828{/,file:///E:/ccidev_data/workspace/Voyager/jetty-try2/src/main/webapp/,AVAILABLE}
2016-01-07 07:20:03.568:INFO:oejs.ServerConnector:main: Started ServerConnector@6f80fafe{HTTP/1.1,[http/1.1]}{0.0.0.0:8085}
2016-01-07 07:20:03.568:INFO:oejs.Server:main: Started @3915ms
So far - so good. However when I have an injectable component I get
WARNING: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 3
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=Injectable,parent=Service1,qualifiers={},position=-1,optional=false,self=false,unqualified=null,340210621)
at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:75)
at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:211)
at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:234)
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:357)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
at org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:162)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2072)
at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:767)
here is my code:
public static void main(final String[] args) throws Exception {
String webOrverrideXMFile = null;
final Path start = Paths.get("");
final int maxDepth = 5;
try (final Stream<Path> stream = Files.find(start, maxDepth, (path, attr) -> path.getFileName().toString()
.contains("web.xml"))) {
webOrverrideXMFile = stream.findFirst().get().getParent().getParent().toString();
}
catch (final Exception e) {
System.out.println("could not find web override file");
e.printStackTrace();
}
final WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setResourceBase(webOrverrideXMFile);
final Server jettyServer = new Server(8085);
jettyServer.setHandler(webapp);
final ServletHolder jerseyServlet = webapp
.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
// Tells the Jersey Servlet which REST service/class to load.
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", Service1.class.getCanonicalName());
try {
jettyServer.start();
jettyServer.join();
}
catch (final RuntimeException e) {
e.printStackTrace();
}
finally {
// jettyServer.destroy();
}
}