3 Replies Latest reply on Aug 9, 2008 5:38 PM by Kamil Burzynski

    Newbie: how to interface EJB code with external libraries?

    Kamil Burzynski Newbie


      Lets assume, that there is application written in pure JSP, it is using server-side code extensively (creates a lot of background threads, uses few external libraries, does a lot of processing) and regular JSP/servlet web ui. As the application is getting bigger and bigger, more requirements are to be met - clustering, more ajax, failover etc. It seems, that the natural extension would be to migrate to EJB (and JSF for ui side). Now, here is the question: if existing code does use external library, and that library does launch new threads, open some sockets and fire callbacks when it receives some data, how to interface it with EJB?
      To be more specific:
      - the application does some stock transaction processing
      - there is Trade object in JSP
      - there is StockServer object in JSP
      - StockServer does use external library to receive stock prices and confirmation about trades
      - when new trade is placed, new Trade object is created, stored to DB and some messages between StockServer and external library are passed, both ways.

      Now, with EJB, it seems that Trade should become entity bean being stored to DB as well, but how to reorganize StockServer class? I am reading through "Enterprise Java Beans 3.0" book by B. Burke and R. Monson-Haefel and so far I've found only information that I am supposed to not create any new threads from bean code. So, what is 'usual' attempt to create system like the above?

        • 1. Re: Newbie: how to interface EJB code with external librarie
          Sandor Fiedler Newbie

          Well let's try to answer that.

          Think of EJB's as a facade in front of any complex functionality that should be executed remote and/or local with transactions and in a multi user environment. Stateless and stateful session beans are doing something for you while entities (pre EJB 3.0 entity beans) are business object which (mostly) get stored into a database.

          So for example you can write a (stateful) session bean that manages the login and logoff off users. So here is the (at least for me) very simple hint for using EJB's: Keep things simple. Each ejb must have its own clear context.

          For transactions (I think transactions are important for a trade system) there is nothing to do. You get them right out of the box if you're using container managed transactions.

          For the ui by using jsf I personally prefer having one backing bean for one screen (i.e. login/logoff-ui). This backing bean can delegate its work to a ejb (that login facade mentioned above).

          To concluse I would recommend to analyse the system carefully and do design the new app by using a combination of JSF and EJB.

          Hope this helps in a way.

          • 2. Re: Newbie: how to interface EJB code with external librarie
            Kamil Burzynski Newbie

            First of all, thanks for the reply, it has been insightful, really.
            Unfortunately it do not answer my main concern - how to interface with external libraries. In meantime I am progressing with reading the mentioned book, and I am after chapter describing JMS basics. I thought, that it provides solution to my problem.. I was thinking about something like this:

            Right now I do link with some external jar and create one of its objects, lets call it Server. This object does internally open some sockets, run threads and so on, all hidden from me. Then, I register listener on its events and eventually start receive events by my listeners. In meantime I call some methods there to invoke some operations. While I call like up to 100 methods per second, I need to receive like 1000+ events per second, all from external thread.

            Now, how should I approach this specific problem (being abstract from jsf etc. for a moment)?

            I thought I will use JMS. Actually, it do give me all required functionality. I just create two message channels and create standalone application outside of JBoss to interface with my library. The events from the lib will be converted to JMS message, so would be my calls (well, ok I would end up reinventing RMI, nevermind, in theory I would be able to call RMI one side and get JMS in the other direction). But, when I made some quick tests (based on exercises from the book), the amount of overhead is overwhelming (well, 2 separate java vm processes communicating via tcp cannot be so fast): I was able to process like 40 messages per second on my machine. So, about 20-30 times slower than I need. Not to mention, that current, 'native', solution does not use more than 10% cpu... So, is there any better way of solving my problem?

            • 3. Re: Newbie: how to interface EJB code with external librarie
              Kamil Burzynski Newbie

              Well, actually being able to run my code inside JBoss vm, but outside of any bean, could get enough speed, even with usage of jms (just by reduction of tcp connection and replacing it with local). This is definitely possible, as I can do that as Tomcat servlet (i.e. servlet which does use external libraries and communicates with it heavily). Is there simple method of putting such code to JBoss?