[jboss-dev-forums] [JBoss AS7 Development] - Re: Messaging configuration in AS 7
Tom Jenkinson
do-not-reply at jboss.com
Wed Jul 13 04:30:38 EDT 2011
Tom Jenkinson [http://community.jboss.org/people/tomjenkinson] created the discussion
"Re: Messaging configuration in AS 7"
To view the discussion, visit: http://community.jboss.org/message/614825#614825
--------------------------------------------------------------
Is there an example of best practice for the route you suggested? Lets say I deployed a dummy EJB so that I could get an MDB:
public interface MyFakeMDB extends javax.jms.MessageListener {
}
@Stateless
@Singleton
public class MyFakeMDBImpl MyFakeMDB {
public void onMessage(Message message) {
}
}
Assuming this is totally a fake EJB and I just want it to have MDB like behavior, where should I put the JMS code to bind my message listener to the queue.
I don't mean what code do I need to write:
MessageConsumer mc = ((ConnectionFactory) new InitialContent().lookup("ConnectionFactory")).createConnection().createSession().createMessageConsumer((Destination)new InitialContext().lookup("foo"));
mc.setMessageListener(this);
What I mean is, where should this code go? I guess the constructor, but with AS7, will the EJB constructor only be called when someone accesses my the EJB through an interface, or is an instance of my EJB always created as soon as I deploy my ear/war?
<quick grep of the internet/>
Actually, I just checked online and can see that I can put this in the constructor,and put the following annotation as well:
javax.ejb.Startup
Which leads to the following:
public interface MyFakeMDB extends javax.jms.MessageListener {
}
@Stateless
@Singleton
@Startup
public class MyFakeMDBImpl implements MyFakeMDB {
@Inject
Context ic;
MessageConsumer mc;
public MyFakeMDBImpl() {
ConnectionFactory cf = (ConnectionFactory) ic.lookup("ConnectionFactory");
Connection c = cf.createConnection();
Session s = c.createSession();
Destination d = (Destination)ic.lookup("foo");
mc = s.createMessageConsumer(d);
mc.setMessageListener(this);
}
public void onMessage(Message message) {
}
}
Does that look like the best way to do this? I suppose the only issue with this would be if I wanted JTA? In which case I would end up writing quite a bit more code to manage "XAConnectionFactory" code?
--------------------------------------------------------------
Reply to this message by going to Community
[http://community.jboss.org/message/614825#614825]
Start a new discussion in JBoss AS7 Development at Community
[http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2225]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-dev-forums/attachments/20110713/26977751/attachment.html
More information about the jboss-dev-forums
mailing list