File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

public void onMessage(TextMessage msg){}

 
Kris Reid
Ranch Hand
Posts: 247
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 250
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kris Reid
Ranch Hand
Posts: 247
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 250
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
########################################################

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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic