by admin ~ October 14, 2008
Following is the example from the chapter “Message-Driven Bean” from the book “Enterprise JavaBeans 3.0″ by Bill Bruke & Richard Monson-Haefel. This example I have tried it out on JBoss AS 5.0.0.CR1. I have used Postgresql as the database. And I have used eclipse WTP as IDE.
Example 1: Message-Driven Beans
Client 1: JmsClient_ReservationProducer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
| package com.titan.clients;
import javax.jms.MapMessage;
import javax.jms.Session;
import javax.jms.ConnectionFactory;
import javax.jms.Connection;
import javax.jms.Session;
import javax.jms.Queue;
import javax.jms.MessageProducer;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Date;
import com.titan.processpayment.*;
import com.titan.access.DataAccess;
public class JmsClient_ReservationProducer {
public static void main(String[] args) throws Exception {
if (args.length != 2)
throw new Exception(
"Usage: java JmsClient_ReservationProducer <CruiseID> <count>");
Integer cruiseID = new Integer(args[0]);
int count = new Integer(args[1]).intValue();
Context jndiContext = getInitialContext();
DataAccess access = (DataAccess) jndiContext
.lookup("TitanCruises/DataAccessBean/remote");
access.initializeDB();
try {
access.makePaymentDbTable();
} catch (Exception ignored) {
}
ConnectionFactory factory = (ConnectionFactory) jndiContext
.lookup("ConnectionFactory");
Queue reservationQueue = (Queue) jndiContext
.lookup("queue/titan-ReservationQueue");
Queue ticketQueue = (Queue) jndiContext
.lookup("queue/titan-TicketQueue");
Connection connect = factory.createConnection();
Session session = connect
.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer sender = session.createProducer(reservationQueue);
for (int i = 0; i < count; i++) {
int j = 1;
int k = 26;
MapMessage message = session.createMapMessage();
message.setJMSReplyTo(ticketQueue); // Used in ReservationProcessor
// to send Tickets back out
message.setStringProperty("MessageFormat", "Version 3.4");
message.setInt("CruiseID", cruiseID);
//message.setInt("CustomerID", i % 2 + 1); // either Customer 1 or
// 2, all we've got in
// database
message.setInt("CustomerID", j);
j = j + 5;
//message.setInt("CabinID", i % 5 + 1); // cabins 100-109 only
message.setInt("CabinID", k++);
message.setDouble("Price", (double) 1000 + i);
// the card expires in about 30 days
//
Date expDate = new Date(System.currentTimeMillis() + 30 * 24 * 60
* 60 * 1000L);
message.setString("CreditCardNum", "5549861006051975");
message.setLong("CreditCardExpDate", expDate.getTime());
message.setString("CreditCardType", CreditCardDO.MASTER_CARD);
System.out.println("Sending reservation message #" + i);
sender.send(message);
}
connect.close();
}
public static Context getInitialContext()
throws javax.naming.NamingException {
return new InitialContext();
}
} |
Client 2: JmsClient_TicketConsumer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
| package com.titan.clients;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.ConnectionFactory;
import javax.jms.Connection;
import javax.jms.Session;
import javax.jms.Session;
import javax.jms.Queue;
import javax.jms.MessageConsumer;
import javax.jms.JMSException;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.titan.travelagent.TicketDO;
public class JmsClient_TicketConsumer implements javax.jms.MessageListener {
public static void main(String[] args) throws Exception {
new JmsClient_TicketConsumer();
while (true) {
Thread.sleep(10000);
}
}
public JmsClient_TicketConsumer() throws Exception {
Context jndiContext = getInitialContext();
ConnectionFactory factory = (ConnectionFactory) jndiContext
.lookup("ConnectionFactory");
Queue ticketQueue = (Queue) jndiContext
.lookup("queue/titan-TicketQueue");
Connection connect = factory.createConnection();
Session session = connect
.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer receiver = session.createConsumer(ticketQueue);
receiver.setMessageListener(this);
System.out.println("Listening for messages on titan-TicketQueue...");
connect.start();
}
public void onMessage(Message message) {
try {
ObjectMessage objMsg = (ObjectMessage) message;
TicketDO ticket = (TicketDO) objMsg.getObject();
System.out.println("********************************");
System.out.println(ticket);
System.out.println("********************************");
} catch (JMSException displayed) {
displayed.printStackTrace();
}
}
public static Context getInitialContext()
throws javax.naming.NamingException {
return new InitialContext();
}
} |
jndi.properties
1
2
3
| java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=202.141.151.148:1099 |
Address.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| package com.titan.domain;
import javax.persistence.*;
@Entity
public class Address implements java.io.Serializable {
private int id;
private String street;
private String city;
private String state;
private String zip;
@Id @GeneratedValue
public int getId() { return id;}
public void setId(int id) { this.id = id; }
public String getStreet() { return street; }
public void setStreet(String street) { this.street = street; }
public String getCity() { return city; }
public void setCity(String city) { this.city = city; }
public String getState() { return state; }
public void setState(String state) { this.state = state; }
public String getZip() { return zip; }
public void setZip(String zip) { this.zip = zip; }
} |
Cabin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| package com.titan.domain;
import javax.persistence.*;
@Entity
public class Cabin implements java.io.Serializable {
private int id;
private String name;
private int bedCount;
private int deckLevel;
private Ship ship;
@Id @GeneratedValue
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getBedCount() { return bedCount; }
public void setBedCount(int count) { this.bedCount = count; }
public int getDeckLevel() { return deckLevel; }
public void setDeckLevel(int level) { this.deckLevel = level; }
@ManyToOne
@JoinColumn(name="SHIP_ID")
public Ship getShip() { return ship; }
public void setShip(Ship ship) { this.ship = ship; }
} |
CreditCard.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
| package com.titan.domain;
import javax.persistence.*;
import java.util.Date;
@Entity
public class CreditCard implements java.io.Serializable {
private int id;
private Date expirationDate;
private String number;
private String nameOnCard;
private Customer customer;
private CreditCompany creditCompany;
@Id @GeneratedValue
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public Date getExpirationDate() { return expirationDate; }
public void setExpirationDate(Date date) { expirationDate = date; }
public String getNumber() { return number; }
public void setNumber(String number) { this.number = number; }
public String getNameOnCard() { return nameOnCard; }
public void setNameOnCard(String name) { nameOnCard = name; }
@OneToOne(mappedBy="creditCard")
public Customer getCustomer() { return customer; }
public void setCustomer(Customer customer) { this.customer = customer; }
@ManyToOne
public CreditCompany getCreditCompany() { return creditCompany; }
public void setCreditCompany(CreditCompany creditCompany) { this.creditCompany = creditCompany; }
} |
CreditCompany.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| package com.titan.domain;
import javax.persistence.*;
import java.util.*;
@Entity
public class CreditCompany implements java.io.Serializable {
private int id;
private String name;
private Address address;
@Id @GeneratedValue
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
@OneToOne(cascade={CascadeType.ALL})
@JoinColumn(name="ADDRESS_ID")
public Address getAddress() { return address; }
public void setAddress(Address address) { this.address = address; }
} |
Cruise.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| package com.titan.domain;
import java.util.*;
import javax.persistence.*;
@Entity
public class Cruise implements java.io.Serializable {
private int id;
private String name;
private Ship ship;
private Collection<Reservation> reservations = new ArrayList<Reservation>();
public Cruise() {}
public Cruise(String name, Ship ship) {
this.name = name;
this.ship = ship;
}
@Id @GeneratedValue
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName( ) { return name; }
public void setName(String name) { this.name = name; }
@ManyToOne
public Ship getShip() { return ship; }
public void setShip(Ship ship) { this.ship = ship; }
@OneToMany(mappedBy="cruise")
public Collection<Reservation> getReservations() { return reservations; }
public void setReservations(Collection<Reservation> res) { reservations = res; }
} |
Customer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
| package com.titan.domain;
import javax.persistence.*;
import java.util.*;
@Entity
public class Customer implements java.io.Serializable {
private int id;
private String firstName;
private String lastName;
private boolean hasGoodCredit;
private Address address;
private Collection<Phone> phoneNumbers = new ArrayList<Phone>();
private CreditCard creditCard;
private Collection<Reservation> reservations = new ArrayList<Reservation>();
@Id @GeneratedValue
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
public boolean getHasGoodCredit() { return hasGoodCredit; }
public void setHasGoodCredit(boolean flag) { hasGoodCredit = flag; }
@OneToOne(cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
@JoinColumn(name="ADDRESS_ID")
public Address getAddress() { return address; }
public void setAddress(Address address) { this.address = address; }
@OneToOne(cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
public CreditCard getCreditCard() { return creditCard; }
public void setCreditCard(CreditCard card) { creditCard = card; }
@OneToMany(cascade={CascadeType.ALL})
@JoinColumn(name="CUSTOMER_ID")
public Collection<Phone> getPhoneNumbers() { return phoneNumbers; }
public void setPhoneNumbers(Collection<Phone> phones) { this.phoneNumbers = phones; }
@ManyToMany(mappedBy="customers", cascade=CascadeType.REFRESH)
public Collection<Reservation> getReservations() { return reservations; }
public void setReservations(Collection<Reservation> reservations) { this.reservations = reservations; }
} |
Name.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| package com.titan.domain;
public class Name {
private String first;
private String last;
public Name(String first, String last) {
this.first = first;
this.last = last;
}
public String getFirst() { return this.first; }
public String getLast() { return this.last; }
} |
Phone.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| package com.titan.domain;
import javax.persistence.*;
@Entity
public class Phone implements java.io.Serializable {
private int id;
private String number;
private byte type;
@Id @GeneratedValue
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getNumber() { return number; }
public void setNumber(String number) { this.number = number; }
public byte getType() { return type; }
public void setType(byte type) { this.type = type; }
} |
Reservation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
| package com.titan.domain;
import javax.persistence.*;
import java.util.*;
@Entity
public class Reservation implements java.io.Serializable {
private int id;
private Date date;
private double amountPaid;
private Cruise cruise;
private Set<Cabin> cabins = new HashSet<Cabin>();
private Set<Customer> customers = new HashSet<Customer>();
public Reservation() {}
public Reservation(Customer customer, Cruise cruise,
Cabin cabin, double price, Date dateBooked) {
setAmountPaid(price);
setDate(dateBooked);
setCruise(cruise);
Set cabins = new HashSet();
cabins.add(cabin);
this.setCabins(cabins);
Set<Customer> customers = new HashSet<Customer>();
customers.add(customer);
this.setCustomers(customers);
}
@Id @GeneratedValue
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public Date getDate() { return date; }
public void setDate(Date date) { this.date = date; }
public double getAmountPaid() { return amountPaid; }
public void setAmountPaid(double amount) { amountPaid = amount; }
@ManyToOne
@JoinColumn(name="CRUISE_ID")
public Cruise getCruise() { return cruise; }
public void setCruise(Cruise cruise) { this.cruise = cruise; }
@ManyToMany
@JoinTable(name="RESERVATION_CABIN",
joinColumns={@JoinColumn(name="RESERVATION_ID")},
inverseJoinColumns={@JoinColumn(name="CABIN_ID")})
public Set<Cabin> getCabins() { return cabins; }
public void setCabins(Set<Cabin> cabins) { this.cabins = cabins; }
@ManyToMany
@JoinTable(name="RESERVATION_CUSTOMER",
joinColumns={@JoinColumn(name="RESERVATION_ID")},
inverseJoinColumns={@JoinColumn(name="CUSTOMER_ID")})
public Set<Customer> getCustomers() { return customers; }
public void setCustomers(Set<Customer> customers) { this.customers = customers; }
} |
Ship.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| package com.titan.domain;
import javax.persistence.*;
@Entity
public class Ship implements java.io.Serializable {
private int id;
private String name;
private double tonnage;
public Ship() {}
public Ship(String name, double tonnage) {
this.name = name;
this.tonnage = tonnage;
}
@Id @GeneratedValue
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public < |