It's not a secret anymore!
The moose likes EJB and other Java EE Technologies and the fly likes Filewriting in EJB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Filewriting in EJB" Watch "Filewriting in EJB" New topic

Filewriting in EJB


Joined: Jul 29, 2002
Posts: 24
Hi all
Can anybody tell me , whether file writing is possible in EJB or not? Because as per the specification it is mentioned that its not a good practice. In that case if i want to log an error from the bean what would be that best way? Also i have one requirement for creating a file at runtime. For that if i choose bean, will it create any problem ? or tell me its possible..
Rishi Singh
Ranch Hand

Joined: Dec 09, 2000
Posts: 321
Hi george,
File Input/Output are not advisable as per EJB specs, though you can do it, no one stops you from reading from a file or any IO operations.Things may go hunky-dory when then network connections opened by you (if it) clashes with that opened by the should be very careful to study how the container manages the IO operations before doing it programatically.
As regards creating a file at runtime, I think it should be encapsulated in a JavaBean or a helper class and not a EnterpriseBean and if required you can call a normal bean from a EJB (put the Java Bean reference entry into <ejb-ref>
and map the same).
Rishi Singh
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17421

Although doing file I/O in an EJB probably won't cause you to crash and burn, it won't win you any prizes, either. Sun lists a number of reasons why it's a Bad Thing in the EJB spec.
A good way to look at an EJB method is to consider an EJB to be rather like a Windows DLL. It's something you call rather than something that does work on its own, and you should furthermore keep the work done in a call to be short and fast.
However, in a DLL, the methods can spawn threads to maintain state and do background processing. EJBs aren't allowed to do this. So if you need "heavy lifting", do it in a servlet or a task service outside the EJB.

An IDE is no substitute for an Intelligent Developer.
Vladimir Ergovic
Ranch Hand

Joined: Apr 22, 2001
Posts: 63
If you would like to write files or log from enterprise beans use Message Driven Bean wich reads the que and write to file so do some JMS from the client and it should be fine

Vladimir Ergovic
I agree. Here's the link:
subject: Filewriting in EJB
It's not a secret anymore!