Chapter 23 908 Example Message-Driven Bean This message-driven bean acts as a message listener on a destination (either a topic or a queue) and prints out messages that it receives. Save this code as MyMessageBean.java: package mailbean; import javax.ejb.*; import javax.jms.*; import javax.naming.*; public class MyMessageBean implements MessageDrivenBean, MessageListener {     /**      * These methods are required by EJB Specification      * but we don't use them      */     public void ejbActivate(){}     public void ejbRemove(){}     public void ejbPassivate(){}     public void ejbCreate () throws CreateException{}     public void setMessageDrivenContext(MessageDrivenContext ctx){}     /**     * Message listener interface.     */     public void onMessage(Message message)     {         try {             String text;             if(message instanceof TextMessage) {                 text = ((TextMessage)message).getText();             }else{                 text = message.toString();             }             System.out.println("Message Received: "+ text);     }         catch(JMSException jmse){             jmse.printStackTrace();       }     } } MyMessageBean waits for messages on a destination (either a topic or a queue). The developer specifies the destination in the bean's deployment descriptor. If the developer specifies the destination as MyJMSTopic, this bean will perform the same function as the Subscriber from the Pub/Sub examples. If the developer specifies the destination as MyJMSQueue, this bean will perform the same function as the Receiver from the PTP examples. Notice that this bean takes much less code to implement than either the Subscriber or the Receiver and it is flexible enough to act as either.