Hi,
I have written an Message driven bean and a client.
When I run client it gets executed, but I do not see any output at bean.
Below is the code.Can some one help me what is wrong with the code? I am able to deploy the code on glassfish server.
------------------------------------The MDB ------------------------------
ackage com.wiley.examples.mdb;
import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class LogBean implements MessageDrivenBean, MessageListener {
/**
*
*/
private static final long serialVersionUID = 7032092859063615127L;
MessageDrivenContext ctx;
public void ejbCreate() throws EJBException {
System.out.println("Call LogBean:ejbCreate()");
}
public void ejbRemove() throws EJBException {
System.out.println("Call LogBean:ejbRemove()");
}
public void setMessageDrivenContext(MessageDrivenContext ctx)
throws EJBException {
System.out.println("Call LogBean:setMessageDrivenContext()");
this.ctx = ctx;
}
public void onMessage(Message msg) {
System.out.println("Call LogBean:onMessage()");
if(msg instanceof TextMessage) {
TextMessage tm = (TextMessage)msg ;
try{
String text = tm.getText();
System.err.println("Received a new message : "+text);
} catch(JMSException e){
e.printStackTrace();
}
}
}
}
------------ejb-jar.xml-------------------------------
<enterprise-beans>
<message-driven>
<ejb-name>LogBean</ejb-name>
<ejb-class>com.wiley.examples.mdb.LogBean</ejb-class>
<transaction-type>Container</transaction-type>
<message-destination-type>javax.jms.Topic</message-destination-type>
<!-- acknowledge-mode>AUTO_ACKNOWLEDGE</acknowledge-mode-->
<resource-ref>
<res-ref-name>jms/connectionFactory</res-ref-name>
<res-type>javax.jms.TopicConnectionFactory</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</message-driven>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>LogBean</ejb-name>
<method-intf>Remote</method-intf>
<method-name>onMessage</method-name>
<method-params>
<method-param>javax.jms.Message</method-param>
</method-params>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
------------------------sun-ejb-jar.xml------------------------------
<ejb>
<ejb-name>LogBean</ejb-name>
<jndi-name>jms/testQueue</jndi-name>
<resource-ref>
<res-ref-name>jms/connectionFactory</res-ref-name>
<jndi-name>jms/connectionFactory</jndi-name>
<default-resource-principal>
<name>guest</name>
<password>guest</password>
</default-resource-principal>
</resource-ref>
<mdb-connection-factory>
<jndi-name>jms/connectionFactory</jndi-name>
<default-resource-principal>
<name>guest</name>
<password>guest</password>
</default-resource-principal>
</mdb-connection-factory>
</ejb>
-------------------------MDB Client------------------------------
package com;
import java.util.Properties;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.InitialContext;
public class MDBClient {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs",
"com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state",
"com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
// optional. Defaults to 3700. Only needed if target orb port is not 3700.
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext context = new InitialContext(props);
TopicConnectionFactory factory = (TopicConnectionFactory)context.lookup("jms/connectionFactory");
TopicConnection connection = factory.createTopicConnection();
TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = (Topic)context.lookup("jms/testQueue");
TopicPublisher publisher = session.createPublisher(topic);
TextMessage msg = session.createTextMessage();
msg.setText("This is a test message");
publisher.publish(msg);
}
}
[Message sent by forum member 'lavkul' (lavkul)]
http://forums.java.net/jive/thread.jspa?messageID=250176