使用 JBoss 和 Spring 在 Java Web 应用程序之间共享业务对象实例的最佳方式是什么? [英] What's the best way to share business object instances between Java web apps using JBoss and Spring?

查看:32
本文介绍了使用 JBoss 和 Spring 在 Java Web 应用程序之间共享业务对象实例的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前有一个 Web 应用程序加载一个 Spring 应用程序上下文,它实例化了一堆业务对象、DAO 对象和 Hibernate.我们希望与另一个 Web 应用程序共享此堆栈,以避免具有相同对象的多个实例.

We currently have a web application loading a Spring application context which instantiates a stack of business objects, DAO objects and Hibernate. We would like to share this stack with another web application, to avoid having multiple instances of the same objects.

我们研究了几种方法;使用 JMX 或 JNDI 或使用 EJB3 公开对象.

We have looked into several approaches; exposing the objects using JMX or JNDI, or using EJB3.

不同的方法都有各自的问题,我们正在寻找一种轻量级的方法.

The different approaches all have their issues, and we are looking for a lightweight method.

有关如何解决此问题的任何建议?

Any suggestions on how to solve this?

我收到了要求我详细说明的评论,所以这里是:

I have received comments requesting me to elaborate a bit, so here goes:

我们要解决的主要问题是我们只想拥有一个 Hibernate 实例.这是由于在运行多个使用相同数据源的客户端应用程序时 Hibernate 的二级缓存失效的问题.此外,业务/DAO/Hibernate 堆栈越来越大,因此不复制它更有意义.

The main problem we want to solve is that we want to have only one instance of Hibernate. This is due to problems with invalidation of Hibernate's 2nd level cache when running several client applications working with the same datasource. Also, the business/DAO/Hibernate stack is growing rather large, so not duplicating it just makes more sense.

首先,我们尝试研究如何将业务层单独暴露给其他 Web 应用程序,并且 Spring 以少量 XML 的价格提供 JMX 包装.但是,我们无法将 JMX 实体绑定到 JNDI 树,因此我们无法从网络应用中查找对象.

First, we tried to look at how the business layer alone could be exposed to other web apps, and Spring offers JMX wrapping at the price of a tiny amount of XML. However, we were unable to bind the JMX entities to the JNDI tree, so we couldn't lookup the objects from the web apps.

然后我们尝试将业务层直接绑定到 JNDI.尽管 Spring 没有为此提供任何方法,但使用 JNDITemplate 绑定它们也很简单.但这导致了几个新问题:1) 安全管理器拒绝访问 RMI 类加载器,因此一旦我们尝试调用 JNDI 资源上的方法,客户端就会失败.2) 一旦安全问题得到解决,JBoss 抛出 IllegalArgumentException: object is not an instance of declaring class.一些阅读表明我们需要 JNDI 资源的存根实现,但这似乎很麻烦(也许 Spring 可以帮助我们?)

Then we tried binding the business layer directly to JNDI. Although Spring didn't offer any method for this, using JNDITemplate to bind them was also trivial. But this led to several new problems: 1) Security manager denies access to RMI classloader, so the client failed once we tried to invoke methods on the JNDI resource. 2) Once the security issues were resolved, JBoss threw IllegalArgumentException: object is not an instance of declaring class. A bit of reading reveals that we need stub implementations for the JNDI resources, but this seems like a lot of hassle (perhaps Spring can help us?)

我们还没有过多地研究 EJB,但是在前两次尝试之后,我想知道我们想要实现的目标是否完全可能.

We haven't looked too much into EJB yet, but after the first two tries I'm wondering if what we're trying to achieve is at all possible.

总结一下我们要实现的目标:一个 JBoss 实例,几个 Web 应用程序在 DAO 层和 Hibernate 之上使用一组业务对象.

To sum up what we're trying to achieve: One JBoss instance, several web apps utilizing one stack of business objects on top of DAO layer and Hibernate.

最好的问候,

尼尔

推荐答案

Web 应用程序是否部署在同一台服务器上?

Are the web applications deployed on the same server?

我不能说 Spring,但使用会话 Bean 将您的业务逻辑移到 EJB 层很简单.

I can't speak for Spring, but it is straightforward to move your business logic in to the EJB tier using Session Beans.

应用程序组织很简单.逻辑进入会话 Bean,这些会话 Bean 作为 Java EE 工件捆绑在单个 jar 中,并带有 ejb-jar.xml 文件(在 EJB3 中,这实际上可能是空的).

The application organization is straight forward. The Logic goes in to Session Beans, and these Session Beans are bundled within a single jar as an Java EE artifact with a ejb-jar.xml file (in EJB3, this will likely be practically empty).

然后将您的实体类捆绑到一个单独的 jar 文件中.

Then bundle you Entity classes in to a seperate jar file.

接下来,您将在各自的 WAR 文件中构建每个 Web 应用.

Next, you will build each web app in to their own WAR file.

最后,所有的 jars 和 wars 都捆绑到一个 Java EE EAR 中,并带有关联的 application.xml 文件(同样,这可能非常小,只需枚举 EAR 中的 jars).

Finally, all of the jars and the wars are bundled in to a Java EE EAR, with the associated application.xml file (again, this will likely be quite minimal, simply enumerating the jars in the EAR).

此 EAR 被批发部署到应用服务器.

This EAR is deployed wholesale to the app server.

每个 WAR 实际上都是独立的——它们有自己的会话、上下文路径等.但它们共享公共 EJB 后端,因此您只有一个二级缓存.

Each WAR is effectively independent -- their own sessions, there own context paths, etc. But they share the common EJB back end, so you have only a single 2nd level cache.

您还使用本地引用和调用语义与 EJB 对话,因为它们位于同一服务器中.这里不需要远程调用.

You also use local references and calling semantic to talk to the EJBs since they're in the same server. No need for remote calls here.

我认为这很好地解决了您遇到的问题,并且在带有 EJB 3 的 Java EE 5 中非常简单.

I think this solves quite well the issue you're having, and its is quite straightforward in Java EE 5 with EJB 3.

此外,据我所知,您仍然可以在大部分工作中使用 Spring,但我不是 Spring 人,所以我无法谈论细节.

Also, you can still use Spring for much of your work, as I understand, but I'm not a Spring person so I can not speak to the details.

这篇关于使用 JBoss 和 Spring 在 Java Web 应用程序之间共享业务对象实例的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆