File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Help in MDB with javax.jms Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Help in MDB with javax.jms" Watch "Help in MDB with javax.jms" New topic
Author

Help in MDB with javax.jms

Manikandan Parthasarathi
Greenhorn

Joined: Jul 29, 2008
Posts: 16
MessageBean.java

//Required Imports
@MessageDriven(activationConfig =
{ @ActivationConfigProperty(propertyName="destinationType",
propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(propertyName="destination",
propertyValue="jms/queue")})
public class MessageBean implements MessageListener {


public void onMessage(Message msg) {
TextMessage tmsg = null;

try {
tmsg = (TextMessage) msg;
System.out.println(tmsg);

System.out.println("The onMessage() is called");
} catch (Exception e) {
e.printStackTrace();
}


}
}

MsgClient.java
//Required Imports

public class MsgClient {
Queue jmsQueue = null;
QueueConnectionFactory factory = null;
QueueConnection connection = null;
QueueSession session = null;
QueueSender sender = null;
TextMessage message=null;
public void createConnection(){

try{

InitialContext ctx = new InitialContext();
jmsQueue = (Queue) ctx.lookup("jms/Queue");
factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
connection=factory.createQueueConnection();
session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
sender=session.createSender(jmsQueue);

}catch(Exception e){
e.printStackTrace();
}


}
public void sendMessage() {

try {
message = session.createTextMessage();
message.setText("***HI***");
System.out.println(message);
sender.send(message);
System.out.println("Sending message");
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

MessageClient.java

import javax.annotation.Resource;
@Resource(mappedName = "jms/Queue")
public class MessageClient {


public static void main(String[] args) {
MsgClient msgcon = new MsgClient();
System.out.println(msgcon.connection);
System.out.println(msgcon.session);
System.out.println(msgcon.sender);
msgcon.createConnection();
msgcon.sendMessage();
}
}


Hi friends the above are my code i am trying to send a simple message to the receiver through jms queue. i am using jboss4.0 version. the error i am getting is

null
null
null
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at com.cts.msg.MsgClient.createConnection(MsgClient.java:23)
at com.cts.msg.MessageClient.main(MessageClient.java:15)
java.lang.NullPointerException
at com.cts.msg.MsgClient.sendMessage(MsgClient.java:38)
at com.cts.msg.MessageClient.main(MessageClient.java:16)



Please Help Me........Thanks in Advance


Regards
Manikandan
SCJP 5 , SCWCD 5
Jerwin Louise Uy
Ranch Hand

Joined: Oct 27, 2007
Posts: 75
The MDB is not accessible outside the container unless you deploy the stand alone client in an EAR or you access the JNDI context by specifying the ORB host and port which varies on application server implementation.

You could try packaging the app client in an EAR so that it can deployed within the container and have access to the context of the MDB.

Regards.


Uy Jerwin Louise Vergara
Junior Developer / Research and Development at Incuventure Partners Corporation
email: jerwin.uy@incuventure.net
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help in MDB with javax.jms
 
Similar Threads
Getting very strange exception: JMS in EJB3, JBoss
How to receive message after sending from client side ?
I can't see ejbCreat(),ejbRemove or initialize() methods in this MDB.
Error while using MDB with JMS
TimeOut in sending and receiving message in MQ5.3 thorugh RAD 8