没有应用程序服务器的 Java EE
从 EJB 3 开始,我们有了可嵌入的 EJB 容器,可以在没有应用程序服务器的情况下使用 JPA 实现,还有用于上下文和依赖项注入的 Weld 等。由于在许多系统上只有 Tomcat 可用,我想知道 Java EE 是否可以在没有应用程序服务器的情况下使用,但可以使用像 Tomcat 这样的 Servlet 容器。
我需要做什么来设置 Java 环境?您看到什么缺点?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请注意,Tomcat 是一个应用程序服务器。也就是说,我们在 10 月份发布了 Apache TomEE,它是添加了缺少的 JavaEE 部分的 Tomcat,然后使用 Oracle 的官方 TCK 进行 Java EE 6 认证。
该堆栈是从过去简单地称为“OpenEJB/Tomcat”的堆栈演变而来的,这是一个有用的堆栈,但名声不好:) 由于“EJB”部分而经常被忽视,同时它还向 Tomcat 提供了事务、JMS、WebServices 等。新名称更好了,现在它像 JBoss 或 GlassFish 一样得到了官方认证。我们对它的未来感到非常兴奋。
Note that Tomcat is an Application Server. That said, in October we released Apache TomEE which is Tomcat with the missing JavaEE parts added, then Java EE 6 certified using the official TCK from Oracle.
The stack evolved from what used to be simply called "OpenEJB/Tomcat", which was a useful stack with a bad name :) Commonly overlooked because of the "EJB" part, meanwhile it also delivered Transactions, JMS, WebServices and more to Tomcat. The new name is far better and now it's officially certified like JBoss or GlassFish. We're pretty excited about its future.
如果我理解得很好,您想在 servlet 容器中使用 EJB3/JPA。
不仅有独立的 JPA 实现,还有可嵌入的 EJB3 容器,例如 OpenEJB 或 Glassfish 可嵌入容器。因此,没有什么可以阻止您从 Servlet 容器启动这样的可嵌入容器来使用 EJB3。
(注意:我不知道有关事务的所有细节。在成熟的应用程序服务器中,您有 JTA 和分布式事务管理器。在 Tomcat 等 Servlet 容器中则没有。JPA 与 JTA 一起工作和普通的 JDBC,但我不知道在没有 JTA 的情况下嵌入式容器是如何工作的,不过,我想这会起作用,因为这样的嵌入式容器也适用于单元测试,我猜没有 JTA 分布式事务管理器。)
另一种方法是使用 Spring。 Spring和EJB3确实变得非常相似。您可以在 Servlet 容器中启动 Spring DI 容器,并或多或少地受益于与 EJB3 相同的功能(声明性事务等)。请参阅这篇关于 Spring 与 EJB3 的文章。
所有这些技术都已成为相当模块化,尤其是 Java EE 配置文件。您可以以某种方式相互独立地使用 Sevlet、EJB3、JMS、JPA,甚至 JTA。您还可以使用 Spring 或 Java EE 创建一个环境,在其中挑选您想要的环境。问题是什么时候它才停止有意义并使用应用程序。服务器提供一切可用且易于管理的服务。我认为 Servlet/EJB3/JPA 是极限,如果需要更多,请选择应用程序。服务器。
If I understand well, you want to use EJB3/JPA within a servlet container.
There are not only stand-alone implementations of JPA, but also embeddable EJB3 container, such as OpenEJB or Glassfish embeddable container. So nothing prevents you from starting such an embeddable container from the Servlet container to use EJB3.
(Note: I don't know all the details about transactions. In a full-blown app. server, you have JTA and a distributed transaction manager. You don't have that in a Servlet container such as Tomcat. JPA works with JTA and plain JDBC, but I don't know exactly how an embeddable container work without JTA. Still, I guess that would work given that such embeddable containers are also meant for unit-testing where I guess there is no JTA distributed transaction manager.)
Another approach would be to use Spring. Spring and EJB3 have indeed become very similar. You can start the Spring DI container within the Servlet container and benefit more or less from the same facilities as EJB3 (declarative transactions, etc.). See this post about Spring vs. EJB3.
All these technologies have become pretty modular, especially with Java EE profiles. You can use Sevlets, EJB3, JMS, JPA, even JTA somehow independently of one other. You can also create an environment where you cherry pick the ones you would like, either with Spring or with Java EE. The question is when does it stop to make sense and rather use an app. server with everything available and easily manageable. I think Servlet/EJB3/JPA is the limit, if more is needed go for an app. server.
您通常需要某种类型的容器,即使该容器不提供与 Java EE 相关的服务。毕竟,您确实需要一个长期存在的 JVM 进程来托管您正在执行的代码。 Tomcat 和 Jetty 可以很好地完成这项工作,除了基本的 servlet 服务之外,还提供一些有用的相关附加功能,例如连接池。
You will generally require some kind of container, even if that container doesn't provide Java EE-related services. After all, you do need a long-lived JVM process to host the code that you're executing. Tomcat and Jetty will do the job nicely, and in addition to basic servlet services, provide a few useful extras that will be relevant, like connection pooling.
没有应用程序服务器的 J2EE 是由我(来自 Atomikos 的 Guy Pardon)几年前通过这篇开创性的文章引入的: http://www.onjava.com/pub/a/onjava/2006/02/08/j2ee-without-application-server.html - 当时专注于JMS和JDBC。
一般来说,由于 Spring 和 Hibernate,设置很容易。事实上,我是在完成一个 Java EE 项目并遇到与应用程序服务器和 EJB 相关的 XML 地狱之后受到启发而这样做的。没有应用程序服务器,事情突然变得更加简单且更易于测试。
如果您需要 Tomcat 安装,那么配置起来可能会比较麻烦,但最近 Atomikos 推出了开箱即用的 Tomcat 集成,作为其商业产品的一部分,网址为 http://www.atomikos.com。
华泰
J2EE without application server was introduced years ago by me (Guy Pardon, from Atomikos), with this seminal article: http://www.onjava.com/pub/a/onjava/2006/02/08/j2ee-without-application-server.html - focused on JMS and JDBC at the time.
In general it's easy to setup thanks to Spring and Hibernate. Actually, I got inspired to do this after doing a Java EE project and being confronted with the XML hell associated with app servers and EJBs. Without application server things suddenly became a lot simpler and more testable.
If you need a Tomcat installation then can be a bit more of a hassle to configure, but recently Atomikos has introduced out-of-the-box Tomcat integration as part of its commercial offering at http://www.atomikos.com.
HTH