Hello,
I am trying to configure Jetty (embedded) to use JTA with Atmikos along with OpenJPA 2.2.0, but I can't seem to get Jetty to recognize my org.eclipse.jetty.plus.jndi.Resource
Java (startup):
final Resource serverXml = Resource.newSystemResource("META-INF/jetty.xml");
final XmlConfiguration configuration = new XmlConfiguration(serverXml.getInputStream());
server = (Server) configuration.configure();
final SelectChannelConnector defaultConnnector = new SelectChannelConnector();
defaultConnnector.setPort(9080);
defaultConnnector.setMaxIdleTime(30000);
defaultConnnector.setRequestHeaderSize(8192);
server.setConnectors(new Connector[] { defaultConnnector });
server.setHandler(new DefaultHandler());
server.setStopAtShutdown(true);
server.start();
server.join();
public class DefaultHandler extends AbstractHandler {
private static final Logger log = LoggerFactory.getLogger(DefaultHandler.class);
public void handle(final String target, final Request baseRequest, final HttpServletRequest request,
final HttpServletResponse response) throws IOException, ServletException {
javax.naming.Context ctx;
try {
ctx = new javax.naming.InitialContext();
// Fails here with "javax.naming.NameNotFoundException: null"
javax.naming.Context envCtx = (javax.naming.Context) ctx.lookup("java:comp/env");
String configArg = (String) envCtx.lookup("jdbc/myDS");
} catch (NamingException e) {
log.error("No JDBC Resource", e);
}
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
//baseRequest.setHandled(true);
response.getWriter().println("<h1>Hello World</h1>");
try {
// Create a new EntityManagerFactory using the System properties.
// The "hellojpa" name will be used to configure based on the
// corresponding name in the META-INF/persistence.xml file
EntityManagerFactory factory = Persistence.
createEntityManagerFactory("test-pu", System.getProperties());
// Create a new EntityManager from the EntityManagerFactory. The
// EntityManager is the main object in the persistence API, and is
// used to create, delete, and query objects, as well as access
// the current transaction
EntityManager em = factory.createEntityManager();
// Begin a new local transaction so that we can persist a new entity
em.getTransaction().begin();
// Create and persist a new Message entity
em.persist(new Message("Hello Persistence!"));
// Commit the transaction, which will cause the entity to
// be stored in the database
em.getTransaction().commit();
// It is always good practice to close the EntityManager so that
// resources are conserved.
em.close();
// Create a fresh, new EntityManager
EntityManager em2 = factory.createEntityManager();
// Perform a simple query for all the Message entities
Query q = em2.createQuery("select m from Message m");
// Go through each of the entities and print out each of their
// messages, as well as the date on which it was created
for (Message m : (List<Message>) q.getResultList()) {
UGateUtil.PLAIN_LOGGER.info(m.getMessage() + " (created on: " + m.getCreated() + ')');
response.getWriter().println("<h3>" + m.getMessage() + " (created on: " + m.getCreated() + ')' + "</h3>");
}
// Again, it is always good to clean up after ourselves
em2.close();
factory.close();
} catch (Throwable t) {
log.error("JPA error: ", t);
}
}
}
META-INF/jetty.xml
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- Add a mapping from name in web.xml to the environment -->
<!-- <New id="map1" class="org.eclipse.jetty.plus.jndi.Link"> -->
<!-- <Arg> -->
<!-- <Ref id='Server' /> -->
<!-- </Arg> -->
<!-- <Arg>jdbc/myDS</Arg> -->
<!-- <Arg>jdbc/myDS</Arg> -->
<!-- </New> -->
<!-- JTA XADataSource -->
<New id="myDS" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>
<Ref id="Server" />
</Arg>
<Arg>jdbc/myDS</Arg>
<Arg>
<New class="com.atomikos.jdbc.AtomikosDataSourceBean">
<Set name="minPoolSize">2</Set>
<Set name="maxPoolSize">20</Set>
<Set name="xaDataSourceClassName">org.h2.jdbcx.JdbcDataSource</Set>
<Set name="xaProperties">
<New class="java.util.Properties">
<Call name="setProperty">
<Arg>databaseName</Arg>
<Arg>testDB</Arg>
</Call>
<Call name="setProperty">
<Arg>serverName</Arg>
<Arg>localhost</Arg>
</Call>
<Call name="setProperty">
<Arg>portNumber</Arg>
<Arg>5435</Arg>
</Call>
<Call name="setProperty">
<Arg>user</Arg>
<Arg>sa</Arg>
</Call>
<Call name="setProperty">
<Arg>password</Arg>
<Arg>sa</Arg>
</Call>
</New>
</Set>
<Set name="UniqueResourceName">jdbc/myDS</Set>
</New>
</Arg>
</New>
</Configure>
META-INF/persistence.xml
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="test-pu" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<!-- managed DataSource -->
<jta-data-source>jdbc/myDS</jta-data-source>
<class>org.ugate.service.entity.jpa.Message</class>
</persistence-unit>
</persistence>
Exception when looking up "java:comp/env":
15:08:45.036 ERROR [qtp144024-32] [o.u.s.w.DefaultServlet:37] No JDBC Resource
javax.naming.NameNotFoundException: null
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:444) ~[jetty-all-8.1.2.v20120308.jar:8.1.2.v20120308]
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:531) ~[jetty-all-8.1.2.v20120308.jar:8.1.2.v20120308]
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:546) ~[jetty-all-8.1.2.v20120308.jar:8.1.2.v20120308]
at org.eclipse.jetty.jndi.java.javaRootURLContext.lookup(javaRootURLContext.java:112) ~[jetty-all-8.1.2.v20120308.jar:8.1.2.v20120308]
at javax.naming.InitialContext.lookup(InitialContext.java:411) ~[na:1.7.0_02]
at org.ugate.service.web.DefaultServlet.handle(DefaultServlet.java:34) ~[bin/:na]
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111) [jetty-all-8.1.2.v20120308.jar:8.1.2.v20120308]
at org.eclipse.jetty.server.Server.handle(Server.java:351) [jetty-all-8.1.2.v20120308.jar:8.1.2.v20120308]
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:454) [jetty-all-8.1.2.v20120308.jar:8.1.2.v20120308]
at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:890) [jetty-all-8.1.2.v20120308.jar:8.1.2.v20120308]
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:944) [jetty-all-8.1.2.v20120308.jar:8.1.2.v20120308]
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:634) [jetty-all-8.1.2.v20120308.jar:8.1.2.v20120308]
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:230) [jetty-all-8.1.2.v20120308.jar:8.1.2.v20120308]
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:77) [jetty-all-8.1.2.v20120308.jar:8.1.2.v20120308]
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:609) [jetty-all-8.1.2.v20120308.jar:8.1.2.v20120308]
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:45) [jetty-all-8.1.2.v20120308.jar:8.1.2.v20120308]
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:599) [jetty-all-8.1.2.v20120308.jar:8.1.2.v20120308]
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:534) [jetty-all-8.1.2.v20120308.jar:8.1.2.v20120308]
at java.lang.Thread.run(Thread.java:722) [na:1.7.0_02]