Simple Java Samples


Message Driven Beans

I was recently looking for a simple example of how to use a message driven bean to send email from a web site. The list of recipients could be quite long, and the hardware was not “big time”. So when sending the cursor would spin for quite a while. To avoid this we decided to use an MDB to asynchronously send the email.

This sample was developed in NetBeans v6.5 on OS X 10.5.6. The server used was the GlassFish v2.2 server that installs with NetBeans v6.5.

The actual code in this sample is very simple stuff but some of the “configurations” were far more arduous than any of the coding.

The jar files are from the distribution folder in the files view of the project and I think you can “create a project” from a jar file. There are two jar files, an MDBSample and a JMS_Tester. The MDB simply prints the contents of a message (either a simple text message or a object formatted to look like an email, imagine that.) to the application server’s console. The JMS_Tester writes two messages (a simple text message and a slightly more complex object) to a JSM queue. The challenge was getting the JMS test client to actually use the queue. There are notes in the header comments of both files.

I hope this sample is more useful than the myriad of samples that I combed through to actually get this to work.