How do I perform I/O operations from a bean?
Created May 4, 2012
Richard Monson-Haefel
Using java.io is prohibited
Using the java.io package from within a bean is prohibited. The EJB 1.1 Specification, section 18.1.2 (Programming Restrictions) states the following:An enterprise bean must not use the java.io package to attempt to access files and directories in the file system. The file system APIs are not well-suited for business components to access data. Business components should use a resource manager API, such as JDBC API, to store data.
The EJB 1.1 specification is available for download from http://java.sun.com/products/ejb/docs.html
Alternative Solutions
To perform I/O operations you must use a resource connection supported by the EJB programming model. EJB servers may support several resource connection options, which can be used to log events as shown below:
- JDBC (javax.sql.DataSource) : Write I/O data in to a relational database.
- URL (java.net.URL) : Write I/O data to a custom server or post them to a web server.
- JavaMail (javax.mail.Session) : E-mail I/O data to a special account
- JMS (javax.jms.QueueConnectionFactory | javax.jms.TopicConnectionFactory) : Send the I/O data as messages.