my dog learned polymorphism*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes  public void onMessage(TextMessage msg){} Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark " public void onMessage(TextMessage msg){}" Watch " public void onMessage(TextMessage msg){}" New topic
Author

public void onMessage(TextMessage msg){}

Kris Reid
Ranch Hand

Joined: Jan 05, 2005
Posts: 247
What will happen when you try to compile the following message driven bean? Assume all packages are correctly imported.

1.public class GatewayBean implements MessageDrivenBean, MessageListener {
2. public GatewayBean(){}
3. public void ejbCreate(){}
4. public void onMessage(TextMessage msg){}
5. public void ejbRemove(){}
6. public void setMessageDrivenContext(MessageDrivenContext ctx){}
7.}

1. Compilation error, incorrect declaration on line 3.

2. Compilation error, incorrect declaration on line 4.

3. The code compiles fine.

4. Compilation error, the GatewayBean class must be declared abstract.

5. Compilation error, the GatewayBean class must be declared final.










Answer 2 is correct.
The message-driven bean class must define one onMessage method whose signature must follow these rules:

The method must be declared as public.
The method must not be declared as final or static.
The return type must be void.
The method must have a single argument of type javax.jms.Message.
The throws clause must not define any application exceptions.




I don't understand why this mthod is declared wrong.
I believe it follows all the above rules....what am I missing?
sawan parihar
Ranch Hand

Joined: Aug 24, 2004
Posts: 250
Hi,
The method signature in the javax.jms.MessageListener is

public void onMessage(Message m);

That means your MDB should provide the implementation of this method because your MDB implements MessageListener . Thats is why point 2 is correct.

Hope that helps.


Sawan<br />SCJP,SCWCD,SCBCD<br /> <br />Every exit is an entry somewhere.
Kris Reid
Ranch Hand

Joined: Jan 05, 2005
Posts: 247
javax.jms.MessageListener defines:
public void onMessage(javax.jms.Message message)


public interface TextMessage extends javax.jms.Message

so isn't
public void onMessage(javax.jms.TextMessage msg){}
leagal?
sawan parihar
Ranch Hand

Joined: Aug 24, 2004
Posts: 250
########################################################

public void onMessage(javax.jms.Message message)

########################################################

Whenever a class implements an interface it has to provide the implementation of the methods declared in the interface or the class will be abstract. If you provide the implementatiom for onMessage(javax.jms.TextMessage) then you will get a compile time error that the MDB needs to implement the inherited abstract method onMessage(javax.jms.Message).

hope that helps
 
Consider Paul's rocket mass heater.
 
subject: public void onMessage(TextMessage msg){}
 
Similar Threads
MessageNotWriteableException - Need to write to JMS Message
question from ejbcertificate mock exam
Where does the ejbCreate definition come from
another mock question
can't find MessageListener class exception