简体   繁体   English

如何在使用EJB3的Java EE环境中嵌入第三方代码

[英]How to embed third party code in a Java EE Environment where EJB3 is used

I am writing a web application which runs on a GlassFish 2.1.1. 我正在编写一个在GlassFish 2.1.1上运行的Web应用程序。 This application contains actually only one stateless session bean, which uses classes from a third party lib. 该应用程序实际上仅包含一个无状态会话bean,该会话bean使用第三方lib中的类。 The instances of this classes are services which create a lot of self managed threads during the runtime. 此类的实例是服务,它们在运行时期间创建许多自管理线程。 I have read in the EJB Restrictions that creating threads in EJB's is not a good idea. 我在EJB限制中读到,在EJB中创建线程不是一个好主意。

I am pretty new to Java EE and EJB but I am wondering how it is possible to use third party libs which are not developed for EJB in a EJB environment. 我对Java EE和EJB还是很陌生,但是我想知道如何在EJB环境中使用不是为EJB开发的第三方库。 I would like to know how i can embed this third party service which is simple a singleton in my Java EE application using EJB in a save way. 我想知道如何以保存方式将这个简单的单例第三方服务嵌入到我的Java EE应用程序中。

The only idea I have is to instantiate the service in a servlet and pass the instance by method call to the EJBs. 我唯一的想法是在servlet中实例化服务,并通过方法调用将实例传递给EJB。 Would this be a better way? 这会是更好的方法吗?

thanks in advance Alex 在此先感谢亚历克斯

A few more details about my project: 有关我的项目的更多详细信息:

The library which i have to use is certainly not made for a EJB Container. 我必须使用的库肯定不是针对EJB容器的。 However, i have no choise. 但是,我没有选择。 I also have changed my desing a bit which i try to explain briefly. 我也改变了我的设计,我试图简要解释一下。 The main purpose of my webapplication is to process jobs. 我的Web应用程序的主要目的是处理作业。 Basicly it is similar to the CI server hudson. 基本上,它类似于CI服务器hudson。 The jobs are something else then build tasks. 这些工作是另外一些事情,然后再建立任务。 However, the jobs can be started over web GUI. 但是,可以通过Web GUI启动作业。 If the user push the start button only a entry with a state is insert in a database table. 如果用户按下“开始”按钮,则仅将具有状态的条目插入数据库表中。 The database table is used as execution queue. 数据库表用作执行队列。 A nother bean, a TimerBean will check the database periodically and call an enginge which is my thridparty lib to process this job. 另一个bean,TimerBean将定期检查数据库并调用enginge(这是我的第三方库)来处理此作业。 The job processing is decoupled of the Client and no transactions are needed. 作业处理与客户端解耦,不需要事务。

  1. Does it keep state between method calls? 它是否在方法调用之间保持状态? Yes, i have to keep the current job in order to cancel the execution. 是的,我必须保留当前作业才能取消执行。 I have solved this with a static variable. 我已经解决了一个静态变量。 I know this is also something i shouldn't do in EJBs but the EJB will never be used for clustering. 我知道这也是我在EJB中不应该做的事情,但是EJB将永远不会用于集群。
  2. Does it access files, open connections? 它访问文件,打开连接吗? The thirdparty library reads and writes files. 第三方库读取和写入文件。
  3. Does it synchronize threads using shared resources, like class variables? 它是否使用类变量等共享资源来同步线程? I don't know the internals of the thirdparty library but i suppose, Yes! 我不知道第三方库的内部结构,但我想是的!

The instances of this classes are services which create a lot of self managed threads during the runtime 此类的实例是服务,在运行时会创建很多自管理线程

Seeing this, a big red alert lamp should go off in your head. 看到这一点,应该在您的头部熄灭一个红色的大警示灯。 This only shows that your library is probably not a good candidate for use with EJB and you should know thoroughly how it works before you proceed with integrating it. 这仅表明您的库可能不是与EJB一起使用的理想选择,并且在继续进行集成之前,您应该彻底了解它的工作方式。

Some other questions you might want to ask: 您可能要问的其他一些问题:

  1. Does it keep state between method calls? 它是否在方法调用之间保持状态?
  2. Does it access files, open connections? 它访问文件,打开连接吗?
  3. Does it synchronize threads using shared resources, like class variables? 它是否使用类变量等共享资源来同步线程?

Since your library probably makes an attempt to scale itself to the environment, one precautionary step might be making sure it's accessed through a singleton EJB (it would at least increase the chance of running correctly on a single VM). 由于您的库可能会尝试将其自身扩展到环境,因此,一个预防措施可能是确保通过单例EJB对其进行访问(这至少会增加在单个VM上正确运行的机会)。 To achieve this on GF 2.1 you need to set it in sun-ejb-jar.xml : 要在GF 2.1上实现此目的,您需要在sun-ejb-jar.xml

<ejb>
    <ejb-name>MyEJB</ejb-name>
    <jndi-name>ejb/MyEJB</jndi-name>
    <bean-pool>
        <steady-pool-size>1</steady-pool-size>
        <max-pool-size>1</max-pool-size>
    </bean-pool>
</ejb>

You could ininialize your library in a @PostConstruct method of MyEJB , no need to use a special servlet for it. 你可以在你的ininialize库@PostConstruct的方法MyEJB ,无需使用特殊的Servlet它。

It's just a sketch of the solution, everything depends on how the library really works. 这只是解决方案的草图,一切都取决于库的实际工作方式。

强烈建议不要产生自己的线程,但是如果您根本不使用事务并且您的应用程序确实可以运行,请不要担心太多

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

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