Book Image

EJB 3 Developer Guide

By : Michael Sikora
Book Image

EJB 3 Developer Guide

By: Michael Sikora

Overview of this book

Table of Contents (18 chapters)
EJB 3 Developer Guide
Credits
About the Author
About the Reviewers
Preface
Annotations and Their Corresponding Packages

A Simple Message-Driven Bean Example


For this example we revert to the banking scenario. Now we will get a session bean to send a message to a JMS queue requesting that a new customer be added to the database. The message will be a Customer object with id, firstName, and lastName attributes. On receipt of the message, the MDB simply adds the customer to the database.

A Session Bean Queue Producer

We have modified the BankServiceBean.addCustomer() method so that it acts as a queue producer. The addCustomer() method is invoked synchronously by a client in the usual manner. The code for addCustomer() is shown below:

@Stateless
public class BankServiceBean implements BankService {
@Resource(mappedName="BankServiceConnectionFactory")
private ConnectionFactory cf;
@Resource(mappedName="BankServiceJMSQueue")
private Queue queue;
...
public void addCustomer(int custId, String firstName,
String lastName) {
try{
Customer cust = new Customer();
cust.setId(custId);
cust.setFirstName(firstName);
cust...