Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Pre-preparing Stateful Beans  RSS feed

 
Jeff Storey
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi - I have a multi-user system that uses EJBs deployed on JBoss. One of the stateful beans takes a long time to initialize (on the order of 15 seconds, but that is long if a user is waiting). Is there a way to initialize this bean before a user actually requests it, so it is sitting on the server doing its initialization while no-one is using it?

Thanks,
Jeff
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you could create a pool of them. Then client's just grab one already created from the pool. Or re-look at your design and determine if you really really need to have all that stuff pre-loaded.

Mark
 
Jeff Storey
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark. When you say to pool the beans though, is there a way to automatically instaniate one copy before the first user logs on (it only gets instantiated currently when the user requests one)?
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess Mark meant pool of resources your stateful bean initialize. You cannot pool the Stateful bean. It helps if you can share more info on what is being done in bean create method and resources used by the bean.
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you initialize objects in your create method that could just as well have been initialized in a business method?

Do you query a database (via JDBC or EntityBeans) in your create method?
 
Jeff Storey
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I do not. I do a lot of heavy-duty computational work that requires an engine in the bean. The engine takes a little while to initialize.
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And this engine has to be part of the Stateless SB? Or is it possible to move it outside the bean? So that, when ejbCreate is called, initialization of the engine is delegeted elsewhere?
 
Jeff Storey
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It could be delegated elsewhere ... I'm just not sure how to go about that. Would it be in a different bean? Also, that leads me to another quick question - is it expected that an EJB system could hypothetically function outside of an ejb container or is it generally accepted to lock it in to working in an ejb container? Thanks again!
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Second question first: You are free to put your business logic outside EJBs, in those new POJO classes. Steer away from entity beans, and you have yourself a nice set of POJO classes in the back, and a facade of SBs and MDBs, whose job is to manage access, security, and transactions.

Now, to your engine. You cannot start a new thread initializing it from your SB, but you can send a message to a MessageListener, telling it to initialize it. This works well if your engine is a singleton, as the SB would then know which engine to call. If this is not possible - i.e. if the engine performs heavy computations, and several clients wish to use it, having a singleton is not the best. But you could have a singleton Map storing references to the engine, with the Map key generated by the SB. The key is stored in the SB during ejbCreate, and retrieved by the business methods at need.



There would be challenges as to error handling - such as, if there is no engine on the Map, is that because initialization isn't done yet, or because initalization failed? You could do that by comparaing when you sent the message to the current time - and if more than, say, 15 or 20 seconds have passed and there is no engine on map, re-initialize (either via message and tell user to wait, or make user wait by doing it in the SB)
[ October 20, 2006: Message edited by: �dne Brunborg ]
 
Jeff Storey
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks - you have been really helpful with this. I will be trying this out in the next day or so.
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I aim to solve

Just let me know how it worked...
 
Jeff Storey
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will let you know how it goes. It looks like that might be delayed as I now have a new problem that maybe you could lend a hand on. My server sends status messages back to my client by publishing them to topic and then the client gets them. However, after a large number of messages I start getting errors on the server saying
OutOfMemoryError: unable to create new native thread
I've changed my server so it only opens one topic session and only creates one publisher and that is only opened once at the start of the server's lifetime and closed afterwards. This way new publisher threads are not being created for each message, but this did not seem to fix the problem. Any thoughts?
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First thought: You're not closing your threads properly.

I suggest you post a new message, e.g. in the Java Intermediate forum, asking there. Ideally, include some example code.
 
Jeff Storey
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a way using the JBoss ClassPreloadService to preload the classes I need. I didn't actually need it as a bean, so I load it more as a utility class, and I think that's all working now. Thanks again for your help.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!