简体   繁体   中英

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. This application contains actually only one stateless session bean, which uses classes from a third party 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.

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. 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.

The only idea I have is to instantiate the service in a servlet and pass the instance by method call to the EJBs. 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. 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. Basicly it is similar to the CI server hudson. The jobs are something else then build tasks. However, the jobs can be started over 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. 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.
  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.

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). To achieve this on GF 2.1 you need to set it in 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.

It's just a sketch of the solution, everything depends on how the library really works.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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