thelordoftherings
Programmer
Hello,
Here is my servlet code:
The problem is that when onMessage invokes I receive NullPointer Exception. Even if I leave this methos blank I still receive this Exception! Can anyone please tell me why?
import java.io.*;
import java.util.*;
import javax.transaction.*;
import javax.naming.*;
import javax.jms.*;
import javax.rmi.PortableRemoteObject;
import javax.servlet.*;
import javax.servlet.http.*;
public class TopicReceiveInTxServlet extends HttpServlet implements MessageListener
{
private final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
private final static String JMS_FACTORY="jms.qaTopicConnectionFactory";
private final static String TOPIC="jms.qaTopic";
private TopicConnectionFactory tconFactory;
private TopicConnection tcon;
private TopicSession tsession;
private TopicSubscriber tsubscriber;
private Topic topic;
private boolean quit = false;
private HttpServletResponse globalResult;
public void doGet(HttpServletRequest request, HttpServletResponse result) throws ServletException, IOException
{
try
{
globalResult = result;
result.setContentType("text/html");
InitialContext ic = getInitialContext("t3://localhost:7001");
init(ic, TOPIC);
System.out.println("JMS Ready To Receive Topic Messages (To quit, send a \"quit\" message).");
// Wait until a "quit" message has been received.
synchronized(this)
{
while (! this.quit)
{
try
{
this.wait();
}
catch (InterruptedException ie)
{
}
}
}
this.close();
}
catch(Exception e)
{
System.out.println("Exception");
}
}
private static InitialContext getInitialContext(String url) throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}
public void close() throws JMSException
{
tsubscriber.close();
tsession.close();
tcon.close();
}
public void init(Context ctx, String topicName) throws NamingException, JMSException
{
tconFactory = (TopicConnectionFactory) PortableRemoteObject.narrow(ctx.lookup(JMS_FACTORY),
TopicConnectionFactory.class);
tcon = tconFactory.createTopicConnection();
tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = (Topic) PortableRemoteObject.narrow(ctx.lookup(topicName), Topic.class);
tsubscriber = tsession.createSubscriber(topic);
tsubscriber.setMessageListener(this);
tcon.start();
}
public void onMessage(Message msg)
{
try
{
String msgText = "";
PrintWriter out = globalResult.getWriter();
if (msg != null)
{
if (msg instanceof TextMessage)
{
System.out.println("here");
//msgText = ((TextMessage)msg).getText();
System.out.println("JMS Topic TextMessage Message Received: " + msgText + "\n");
out.println("JMS Topic TextMessage Received: " + msgText + "<BR>");
}
else if (msg instanceof MapMessage)
{
Enumeration mapNames = ((MapMessage)msg).getMapNames();
String name = (String) mapNames.nextElement();
System.out.println("JMS Topic MapMessage Received: " + name + " \n");
out.println("JMS Topic MapMessage Received: " + name + " <BR>");
}
else if (msg instanceof ObjectMessage)
{
Hashtable numbers = (Hashtable) ((ObjectMessage)msg).getObject();
System.out.println("JMS Topic ObjectMessage Received: " + numbers.toString() + "\n");
out.println("JMS Topic ObjectMessage Received: " + numbers.toString() + "<BR>");
}
else if (msg instanceof BytesMessage)
{
byte byteArray[] = {1,2,3,4,5};
int numBytes = ((BytesMessage)msg).readBytes(byteArray);
System.out.println("JMS Topic BytesMessage Received: " + numBytes + " received \n");
out.println("JMS Topic BytesMessage Received: " + numBytes + " bytes received <BR>");
}
else if (msg instanceof StreamMessage)
{
String s = ((StreamMessage)msg).readString();
System.out.println("JMS Topic StreamMessage Received: " + s + "\n");
out.println("JMS Topic StreamMessage Received: " + s + "<BR>");
}
else if (msg instanceof Message)
{
System.out.println("An empty JMS Topic Message Received: " + "\n");
System.out.println("It's priority: " + msg.getJMSPriority() + "\n");
System.out.println("It's message ID: " + msg.getJMSMessageID() + "\n");
out.println("An empty JMS Topic Message Received: " + "<BR>");
out.println("It's priority:" + msg.getJMSPriority() + "<BR>");
out.println("It's message ID:" + msg.getJMSMessageID() + "<BR>");
}
if (msgText.equalsIgnoreCase("quit"))
{
synchronized(this)
{
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
}
}
catch (JMSException jmse)
{
jmse.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
Here is my servlet code:
The problem is that when onMessage invokes I receive NullPointer Exception. Even if I leave this methos blank I still receive this Exception! Can anyone please tell me why?
import java.io.*;
import java.util.*;
import javax.transaction.*;
import javax.naming.*;
import javax.jms.*;
import javax.rmi.PortableRemoteObject;
import javax.servlet.*;
import javax.servlet.http.*;
public class TopicReceiveInTxServlet extends HttpServlet implements MessageListener
{
private final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
private final static String JMS_FACTORY="jms.qaTopicConnectionFactory";
private final static String TOPIC="jms.qaTopic";
private TopicConnectionFactory tconFactory;
private TopicConnection tcon;
private TopicSession tsession;
private TopicSubscriber tsubscriber;
private Topic topic;
private boolean quit = false;
private HttpServletResponse globalResult;
public void doGet(HttpServletRequest request, HttpServletResponse result) throws ServletException, IOException
{
try
{
globalResult = result;
result.setContentType("text/html");
InitialContext ic = getInitialContext("t3://localhost:7001");
init(ic, TOPIC);
System.out.println("JMS Ready To Receive Topic Messages (To quit, send a \"quit\" message).");
// Wait until a "quit" message has been received.
synchronized(this)
{
while (! this.quit)
{
try
{
this.wait();
}
catch (InterruptedException ie)
{
}
}
}
this.close();
}
catch(Exception e)
{
System.out.println("Exception");
}
}
private static InitialContext getInitialContext(String url) throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}
public void close() throws JMSException
{
tsubscriber.close();
tsession.close();
tcon.close();
}
public void init(Context ctx, String topicName) throws NamingException, JMSException
{
tconFactory = (TopicConnectionFactory) PortableRemoteObject.narrow(ctx.lookup(JMS_FACTORY),
TopicConnectionFactory.class);
tcon = tconFactory.createTopicConnection();
tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = (Topic) PortableRemoteObject.narrow(ctx.lookup(topicName), Topic.class);
tsubscriber = tsession.createSubscriber(topic);
tsubscriber.setMessageListener(this);
tcon.start();
}
public void onMessage(Message msg)
{
try
{
String msgText = "";
PrintWriter out = globalResult.getWriter();
if (msg != null)
{
if (msg instanceof TextMessage)
{
System.out.println("here");
//msgText = ((TextMessage)msg).getText();
System.out.println("JMS Topic TextMessage Message Received: " + msgText + "\n");
out.println("JMS Topic TextMessage Received: " + msgText + "<BR>");
}
else if (msg instanceof MapMessage)
{
Enumeration mapNames = ((MapMessage)msg).getMapNames();
String name = (String) mapNames.nextElement();
System.out.println("JMS Topic MapMessage Received: " + name + " \n");
out.println("JMS Topic MapMessage Received: " + name + " <BR>");
}
else if (msg instanceof ObjectMessage)
{
Hashtable numbers = (Hashtable) ((ObjectMessage)msg).getObject();
System.out.println("JMS Topic ObjectMessage Received: " + numbers.toString() + "\n");
out.println("JMS Topic ObjectMessage Received: " + numbers.toString() + "<BR>");
}
else if (msg instanceof BytesMessage)
{
byte byteArray[] = {1,2,3,4,5};
int numBytes = ((BytesMessage)msg).readBytes(byteArray);
System.out.println("JMS Topic BytesMessage Received: " + numBytes + " received \n");
out.println("JMS Topic BytesMessage Received: " + numBytes + " bytes received <BR>");
}
else if (msg instanceof StreamMessage)
{
String s = ((StreamMessage)msg).readString();
System.out.println("JMS Topic StreamMessage Received: " + s + "\n");
out.println("JMS Topic StreamMessage Received: " + s + "<BR>");
}
else if (msg instanceof Message)
{
System.out.println("An empty JMS Topic Message Received: " + "\n");
System.out.println("It's priority: " + msg.getJMSPriority() + "\n");
System.out.println("It's message ID: " + msg.getJMSMessageID() + "\n");
out.println("An empty JMS Topic Message Received: " + "<BR>");
out.println("It's priority:" + msg.getJMSPriority() + "<BR>");
out.println("It's message ID:" + msg.getJMSMessageID() + "<BR>");
}
if (msgText.equalsIgnoreCase("quit"))
{
synchronized(this)
{
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
}
}
catch (JMSException jmse)
{
jmse.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}