Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help in MDB with javax.jms

 
Manikandan Parthasarathi
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jerwin Louise Uy
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic