简体   繁体   English

Java EE / Glassfish应用程序逻辑

[英]Java EE/Glassfish Application Logic

I am trying to understand where some of my application logic should go in my Java EE Application. 我试图了解我的Java EE应用程序应该放在哪些应用程序逻辑中。 I am new to Java EE and am looking at loading a lot of unstructured data from a legacy database and building a clean object model for use by my application. 我是Java EE的新手,我正在考虑从遗留数据库加载大量非结构化数据,并构建一个干净的对象模型供我的应用程序使用。 From my investigation I see Java EE apps have 2 components, Enterprise Bean and Web Application components. 根据我的调查,我看到Java EE应用程序有两个组件,Enterprise Bean和Web Application组件。 This part of my application will be responsible for loading the data, building the object model and sending messages via JMS based on the current state of the data to interested parties. 我的应用程序的这一部分将负责加载数据,构建对象模型以及根据数据的当前状态通过JMS向感兴趣的各方发送消息。 The data will be updated by synchronisation with the database and from messages received via JMS from remote Java applications. 数据将通过与数据库同步以及通过JMS从远程Java应用程序接收的消息进行更新。

Is an EJB the correct place for this sort of functionality? EJB是否是这种功能的正确位置? How can I start the initialisation of my object model (main method Java App equivalent)? 如何开始我的对象模型的初始化(主要方法Java App等价物)? What is the best practice for creating a timed event to review the object model and send messages via JMS? 创建定时事件以查看对象模型并通过JMS发送消息的最佳实践是什么?

I have read a number of articles on Java EE, Glassfish, EJB's ... but still don't feel I have a clear picture of where I should be writing this functionality. 我已经阅读了很多关于Java EE,Glassfish,EJB的文章......但仍然觉得我没有清楚地了解我应该在哪里编写这个功能。 Any examples I have seen of EJB tend to be around direct method calls on the beans from client applications. 我见过的EJB的任何例子都倾向于围绕来自客户端应用程序的bean的直接方法调用。

At the moment I feel a Java application may do the job but we are looking at using RMI, and web clients in the future. 目前我觉得Java应用程序可以完成这项工作,但我们正在考虑将来使用RMI和Web客户端。

Java EE is traditionally used for client/server architectural style. Java EE传统上用于客户端/服务器架构风格。 The business logic is implemented in EJB session beans, which are usually invoked either from a web request, a JMS message or a RMI-IIOP remote call. 业务逻辑在EJB会话bean中实现,通常通过Web请求,JMS消息或RMI-IIOP远程调用来调用。

Is an EJB the correct place for this sort of functionality? EJB是否是这种功能的正确位置?

The logic goes into the EJB. 逻辑进入EJB。 But there are different types of EJB. 但是有不同类型的EJB。

How can I start the initialisation of my object model (main method Java App equivalent)? 如何开始我的对象模型的初始化(主要方法Java App等价物)?

There is no such thing as a main method. 没有main方法这样的东西。 But there are still some ways to be perform some processing corresponding to the application deployment and/or undeployment. 但仍有一些方法可以执行与应用程序部署和/或取消部署相对应的某些处理。 You can look at ServletContextListener , Glassfish lifecycle module (non-standard) or maybe the newly introduced Singleton beans and @Startup annotation. 您可以查看ServletContextListenerGlassfish生命周期模块(非标准)或者新引入的Singleton bean和@Startup注释。

What is the best practice for creating a timed event to review the object model and send messages via JMS? 创建定时事件以查看对象模型并通过JMS发送消息的最佳实践是什么?

You can create an EJB Timer which will be called periodically. 您可以创建将定期调用的EJB计时器 If you need the model to be loaded once in memory, I would suggest you look at Singleton beans also. 如果你需要在内存中加载一次模型,我建议你也看一下Singleton bean。 With EJB 3.0, the problem was how to start the timer automatically , but I think they improved that in EJB 3.1 and timers can be started automatically by the application server when the application is deployed using the @Scheduled annotation. 对于EJB 3.0,问题是如何自动启动计时器 ,但我认为它们在EJB 3.1中得到了改进,并且当使用@Scheduled注释部署应用程序时,应用程序服务器可以自动启动计时器。 So it may somehow provide you with a desired starting point as you asked in your previous question. 因此,它可能会以某种方式为您提供您在上一个问题中提出的所需起点。

You can send JMS message from any bean. 您可以从任何bean发送JMS消息。 JMS an external system just like the database. JMS就像数据库一样是一个外部系统。 JMS message are received using special kind of EJB called Message-driven bean (MDB). 使用称为消息驱动bean(MDB)的特殊EJB接收JMS消息。

You application will not be a traditional web-EJB-database client-server application, but it should be feasible with Java EE which is definitively a very flexible model. 您的应用程序将不是传统的Web-EJB数据库客户端 - 服务器应用程序,但它应该是可行的Java EE,它绝对是一个非常灵活的模型。

As you said, the examples tend to involve direct invocation. 正如您所说,这些示例往往涉及直接调用。 In my experience, that's not just the examples. 根据我的经验,这不仅仅是例子。 None of the Java EE*1 apps I've seen use a long living object graph like you describe, instead, they typically operate on a single record (+children/related) in response to a web request, web service call or JMS message. 我见过的Java EE * 1应用程序都没有使用像您描述的长生活对象图,相反,它们通常在单个记录(+子/相关)上运行以响应Web请求,Web服务调用或JMS消息。

Your requirements break that mould and Java EE may not be the best fit. 您的要求打破了这种模式,Java EE可能不是最合适的。 At face value, the kind of code you describe belongs in the EJB Container, but that container lacks a good long-living context to anchor your object graph to. 从表面上看,您描述的代码类型属于EJB容器,但该容器缺少一个良好的长期生成环境来锚定您的对象图。 The Web Container has such a context, but lacks facilities for timers and message handling and such. Web容器具有这样的上下文,但缺少定时器和消息处理等设施。 The cost of abandoning J2EE and using a plain Java application instead is of course to lose the application server's facilities for administration, deployment and monitoring. 放弃J2EE和使用普通Java应用程序的代价当然是丢失应用程序服务器的管理,部署和监视工具。

A good choice might be to reach back to what made the Spring Framework big. 一个很好的选择可能是回到使Spring框架变得更大的原因 I don't know if you're familiar with J2EE's history, but the sudden, huge popularity of the Spring Framework and Hibernate were essentially a community rebellion against the EJB container, versions 1.x / 2.x. 我不知道你是否熟悉J2EE的历史,但Spring Framework和Hibernate的突然大量流行本质上是针对EJB容器的版本1.x / 2.x的社区反叛。 What the Spring WebApplicationContext gave you was a robust, transactional back-end to a web application, taking advantage of MDB's and JTA while otherwise ignoring as much of the EJB Container as possible*2 (and greatly simplifying unit-testing in the process). Spring WebApplicationContext为您提供的是Web应用程序的强大,事务性后端,利用MDB和JTA,同时忽略尽可能多的EJB容器* 2(并大大简化了流程中的单元测试)。 You could take this approach and build your application as a single WAR file and boot up your back-end services with Spring. 您可以采用这种方法并将您的应用程序构建为单个WAR文件,并使用Spring启动后端服务。

An interesting alternative is to ditch the Java EE application server altogether and build your application on top of the OSGi framework. 一个有趣的替代方案是完全抛弃Java EE应用程序服务器并在OSGi框架之上构建应用程序。 This is the "normal Java application" approach, with the OSGi runtime giving you the administration console and hot deployment features that you'd have had to roll your own of otherwise. 这是“普通的Java应用程序”方法,OSGi运行时为您提供管理控制台和热部署功能,否则您必须使用它们。 The missing bits of infrastructure are the timer (use Quartz ) and the Message-Driven Beans (use the JMS API directly). 基础设施的缺失部分是计时器(使用Quartz )和Message-Driven Beans(直接使用JMS API)。 An OSGi application ends up feeling a bit like the Linux kernel and boot process, with services deployed and started according to run levels. OSGi应用程序最终感觉有点像Linux内核和启动过程,并根据运行级别部署和启动服务。 Grab Apache Felix and have a look. 抓住Apache Felix并看一看。

You didn't mention scale. 你没有提到规模。 If the object graph is huge , look into technologies like GigaSpaces or Coherence. 如果对象图很大 ,请查看GigaSpaces或Coherence等技术。

**1) Sun took the '2' out of the acronym with the introduction of EJB3* ** 1)随着EJB3 *的推出,Sun将首字母缩写为'2'

**2) Entity EJBs 2.x were the worst part. ** 2)实体EJB 2.x是最糟糕的部分。 EJB 3 can be viewed as largely an "if you can't beat them, join them" effort to standardize Hibernate.* EJB 3可以被视为主要是“如果你不能打败他们,加入他们”的努力来标准化Hibernate。*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM