AS: Jboss 7
Let's say I have a queue defined in file:
{JBOSS7_HOME}/standalone/configuration/standalone.xml
<jms-queue name="myQueue">
<entry name="java:/queue/myQueue"/>
<durable>true</durable>
</jms-queue>
Question:
How to check programatically number of messages in queue?
You can use following code.
String jndiFactory="org.jboss.naming.remote.client.InitialContextFactory";
String jmsFactory="jms/RemoteConnectionFactory";
String queueName="jms/queue/MyQueue";
String providerUrl="remote://ipAddress:4447";
String username="testuser";
String password="testpassword";
Session qsession = null;
Connection qcon = null;
QueueBrowser browser = null;
ConnectionFactory qconFactory;
InitialContext ctx = null;
int depth=0;
try{
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, jndiFactory);
env.put(Context.PROVIDER_URL, providerUrl);
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);
ctx = new InitialContext(env);
qconFactory = (QueueConnectionFactory) ctx.lookup(jmsFactory);
qcon = qconFactory.createConnection(username, password);
qsession = qcon.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) ctx.lookup(qName);
browser = qsession.createBrowser(queue);
qcon.start();
for (Enumeration e = browser.getEnumeration(); e.hasMoreElements();e.nextElement()) {
depth++;
}
}catch(Exception e){
System.out.println("Some exception :"+e.toString());
}finally {
try {
if(qcon !=null)
qcon.close();
if(qsession !=null)
qsession.close();
if(browser !=null)
browser.close();
} catch (Exception e) {
System.out.println("Some exception :"+e.toString());
}
}
If you want to use an in app approach, I would use the following Session Bean:
import java.util.Enumeration;
import java.util.HashMap;
import javax.annotation.Resource;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.transaction.UserTransaction;
import org.apache.log4j.Logger;
import com.amway.dms.dataobject.event.EventHeader;
import com.amway.dms.dbaccess.event.EventHeaderAccess;
import com.amway.dms.util.common.DBUtils;
import com.amway.dms.util.common.StringUtil;
#Stateless
#TransactionManagement(TransactionManagementType.BEAN)
#Local(SBDMSIntegrationsInternalSenderLocal.class)
#Remote(SBDMSIntegrationsInternalSenderRemote.class)
public class SBQueuCount implements SBQueuCountRemote {
private static final String CONNECTION_FACTORY_NAME = "jms/testCF";
private static final String DESTINATION_NAME = "jms/testQueue";
// jms
#Resource(name = CONNECTION_FACTORY_NAME, mappedName = CONNECTION_FACTORY_NAME)
private ConnectionFactory connectionFactory;
#Resource(name = DESTINATION_NAME, mappedName = DESTINATION_NAME)
private Queue queue;
private static final Logger logger = Logger.getLogger(SBQueuCount.class);
#Override
public void getQueueDetails(String noOfMsgToPrint) {
logger.info("Begin getQueueDetails: noOfMsgToPrint:" + noOfMsgToPrint);
Connection connection;
Session session = null;
try {
connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
int numOnQueue = 0;
QueueBrowser outQueueBrowser = null;
outQueueBrowser = session.createBrowser(queue);
if (outQueueBrowser == null) {
logger.info("outQueueBrowser is null");
}
Enumeration messagesOnQ = outQueueBrowser.getEnumeration();
if (messagesOnQ.hasMoreElements())
logger.info("Queue has elements");
while (messagesOnQ.hasMoreElements()) {
// TextMessage textMessage = (TextMessage) messagesOnQ.nextElement();
// if (StringUtil.isNumeric(noOfMsgToPrint)) {
// if (numOnQueue <= Integer.parseInt(noOfMsgToPrint)) {
// logger.info("EventHeader Message: " + textMessage.toString());
// }
// }
numOnQueue++;
}
outQueueBrowser.close();
logger.info("End number of messages on queue: " + numOnQueue);
} catch (JMSException e) {
logger.info("Error " + e);
e.printStackTrace();
}
}
}
Related
Here, You can see the program which fetch the Leaked Connection count from weblogic. But, I always get it as zero. I have set maximum connection count to 10 from my data source and I run some code which not closes the connection so, Connection Unavailable count is increases to 10 but Liked Connection count is still 0.
So, What to do to increase the count of it because there are liked connections are there by application.
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class Test {
private static final Logger logger = Logger.getLogger(Test.class.getName());
private static final String PROTOCOL = "admin.server.protocol";
private static final String HOST = "admin.server.host";
private static final String PORT = "admin.server.port";
private static final String USERNAME = "admin.server.username";
private static final String PASSWORD = "admin.server.password";
private static final String JNDI_ROOT = "jndi.root";
private static final String DATA_SOURCE = "DataSourceName";
private static final String DATASOURCE_ORACLEDS_JTA = "dataSource-OracleDS_jta";
private static MBeanServerConnection connection;
private static JMXConnector connector;
public static void main(String[] args) throws InterruptedException {
Test test = new Test();
System.out.println(test.isConnectionLeaked());
}
public List<String> getDataSourceNames(){
return Arrays.asList(DATA_SOURCE,DATASOURCE_ORACLEDS_JTA);
}
/*
* Initialize connection to the Domain Runtime MBean Server.
*/
public static void initConnection() throws IOException,
MalformedURLException {
logger.info("Inside initConnection");
InputStream is = ClassLoader.getSystemResourceAsStream("jmx.properties");
Properties props = new Properties();
props.load(is);
String protocol = (String) props.get(PROTOCOL);
Integer portInteger = Integer.valueOf((String) props.get(PORT));
int port = portInteger.intValue();
String jndiroot = (String) props.get(JNDI_ROOT);
String hostname = (String) props.get(HOST);
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot);
Hashtable<String, Object> h = new Hashtable<>();
h.put(Context.SECURITY_PRINCIPAL, (String) props.get(USERNAME));
h.put(Context.SECURITY_CREDENTIALS, (String) props.get(PASSWORD));
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
h.put("jmx.remote.x.request.waiting.timeout", new Long(10000));
connector = JMXConnectorFactory.connect(serviceURL, h);
connection = connector.getMBeanServerConnection();
logger.info("End initConnection");
}
public boolean isConnectionLeaked() {
List<String> dataPoolNames = getDataSourceNames();
boolean isLeaked = false;
try {
initConnection();
ObjectName service = new ObjectName("com.bea:Name=DomainRuntimeService,"
+ "Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
ObjectName[] number_of_servers = (ObjectName[]) connection.getAttribute(service, "ServerRuntimes");
int length = (int) number_of_servers.length;
for (int i = 0; i < length; i++) {
logger.info("Server Instance=" + number_of_servers[i]);
String name = (String) connection.getAttribute(number_of_servers[i], "Name");
ObjectName[] number_of_dbpools = (ObjectName[]) connection.getAttribute(new ObjectName("com.bea:Name="
+ name + ",ServerRuntime=" + name + ",Location=" + name + ",Type=JDBCServiceRuntime"),
"JDBCDataSourceRuntimeMBeans");
int pool_length = (int) number_of_dbpools.length;
for (int x = 0; x < pool_length; x++) {
String poolName = (String) connection.getAttribute(number_of_dbpools[x], "Name");
logger.info("********* PoolName=" + poolName + " ******");
int leakedConnectionCount = (Integer) connection.getAttribute(number_of_dbpools[x],
"LeakedConnectionCount");
logger.info("leakedConnectionCount : " + leakedConnectionCount);
if (leakedConnectionCount > 0) { // Send email alert
isLeaked = true;
}
}
}
} catch (Exception e) {
logger.severe("Exception in isConnectionLeaked method");
logger.severe("Message = " + e.getMessage());
} finally {
try {
if (connector != null) {
logger.info("Connectors JMXConnector.");
connector.close();
}
} catch (IOException e) {
logger.severe(e.getMessage());
}
}
logger.info("End isConnectionLeaked. isLeaked = "+isLeaked);
return isLeaked;
}
}
The Inactive Connection Timeout is set to 0?
am working with a project in which I am trying to call a Servlet that will push XML message to a JMS queue running in my local machine, using java code. You can find the actual Servlet code below, which actually sends the message to the queue.
import java.io.IOException;
import java.io.PrintWriter;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MessageSender
extends HttpServlet
{
private static final long serialVersionUID = 1L;
static PrintWriter out;
public static final String CNN_FACTORY = "jms/alert/connectionFactory";
public static final String QUEUE_NAME = "jms/alert/amlScreenQueue";
public static String currentQueueName;
public static String JMSConnectionFactory;
private QueueConnection qcon;
private QueueSession qsession;
private static QueueSender qsender;
private Queue queue;
private static ObjectMessage om;
private static MessageProducer messageProducer;
public MessageSender() {}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
String JMSQueueName = request.getParameter("JMSQueueName");
currentQueueName = JMSQueueName;
JMSConnectionFactory = request.getParameter("JMSConnectionFactory");
out = response.getWriter();
InitialContext ic = getInitialContext();
init(ic, JMSQueueName);
doPost(request, response);
}
catch (Exception e) {
e.printStackTrace();
}
}
public void init(Context ctx, String queueName)
throws NamingException, JMSException
{
QueueConnectionFactory qconFactory = (QueueConnectionFactory)ctx.lookup(JMSConnectionFactory);
this.qcon = qconFactory.createQueueConnection();
this.qsession = this.qcon.createQueueSession(false, 1);
queue = ((Queue)ctx.lookup(queueName));
QueueConnection qcon = qconFactory.createQueueConnection();
qcon.start();
QueueSession qsession = qcon.createQueueSession(false, 1);
qsender = qsession.createSender(queue);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String textMessage = request.getParameter("textMessage");
try
{
TextMessage msg = qsession.createTextMessage(textMessage);
msg.setStringProperty("JMSXGroupID", "1");
msg.setIntProperty("JMSXGroupSeq", 1);
msg.setBooleanProperty("JMS_IBM_Last_Msg_In_Group", false);
qsender.send(msg);
out.println("Message sent to queue " + currentQueueName);
out.println("");
out.println(msg.getText());
}
catch (JMSException e)
{
e.printStackTrace();
out.println(e.getErrorCode());
try
{
qsender.close();
qcon.close();
}
catch (JMSException e) {
e.printStackTrace();
}
}
finally
{
try
{
qsender.close();
qcon.close();
}
catch (JMSException e) {
e.printStackTrace();
}
}
}
private static InitialContext getInitialContext() throws NamingException
{
return new InitialContext();
}
}
This is the java code that I am using to call the servlet,
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map.Entry;
public class SendMessage {
public static void main(String[] args) throws URISyntaxException{
String line;
StringBuffer buffer = new StringBuffer();
String data = null;
try
{
String filePath = "data/payload.xml";
File dataFile = new File(filePath);
if(dataFile.exists()){
data = new String(Files.readAllBytes(Paths.get(filePath)));
}else{
System.out.println("Not Available");
}
String localUrl = "http://localhost:8080/SimulationTool/MessageSender";
String params = "JMSQueueName=jms/alert/amlScreenQueue&"
+ "JMSConnectionFactory=jms/alert/connectionFactory&"
+ "textMessage=" + data;
System.out.println("PARAMS:::" + params);
URL url = new URL(localUrl);
System.out.println("URL FOUND:::" + url.toURI());
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.connect();
System.out.println("Response Code:::" + ((HttpURLConnection)conn).getResponseCode());
BufferedWriter out = new BufferedWriter( new OutputStreamWriter( conn.getOutputStream() ));
out.write(params);
out.flush();
out.close();
// Response code
System.out.println("Response Code:::" + ((HttpURLConnection)conn).getResponseCode());
// Response values
for (Entry<String, List<String>> header : ((HttpURLConnection)conn).getHeaderFields().entrySet()) {
System.out.println(header.getKey() + "=" + header.getValue());
}
BufferedReader in = new BufferedReader( new InputStreamReader( conn.getInputStream() ) );
String response;
while ( (response = in.readLine()) != null ) {
System.out.println( response );
}
in.close();
}
catch ( MalformedURLException ex ) {
ex.printStackTrace();
}
catch ( IOException ex ) {
ex.printStackTrace();
}
}
}
The problem is its failing with below exception,
Response Code:::200
java.net.ProtocolException: Cannot write output after reading input.
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1312)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1291)
at servlettests.SendMessage.main(SendMessage.java:63)
Its shows response code as 200, so the connection was successful but it fails while getting the outputstream, which is being created to send message to the queue. Pls help me on resolving this issue.
I have this sender class which is sending messages to myQueue.
Sender.java
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.commons.collections.Factory;
public class Sender {
private ConnectionFactory factory = null;
private Connection connection = null;
private Session session = null;
private Destination destination = null;
private MessageProducer producer = null;
String [] messages = new String[] {"Hello ...My name is vaniiiiiiiiiii tanejaaaaaaaaaaaaaa",
"Hello ...My name is priyanka tanejaaaaaaaaaaaaaa",
"Hello ...My name is rahul tanejaaaaaaaaaaaaaa",
"Hello ...My name is popo tanejaaaaaaaaaaaaaa"};
public void sendMessage(){
factory = new ActiveMQConnectionFactory(
"tcp://localhost:61619");
try {
connection= factory.createConnection();
connection.start();
session= connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination=session.createQueue("Parul");
producer = session.createProducer(destination);
TextMessage message = session.createTextMessage();
for(int i=0;i<messages.length;i++){
message.setText(messages[i]);
System.out.println("Sent: " + message.getText());
producer.send(message);
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Sender sender = new Sender();
sender.sendMessage();
}
}
My message is getting published to activemq now we want to read messages from activemq. So i have written class Consumer.
Consumer.java
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.Main;
public class Reciever implements MessageListener{
private ConnectionFactory factory;
private Connection connection;
private Session session;
private Destination destination;
private MessageConsumer consumer = null;
Reciever(){
}
void recieveMessage(){
factory= new ActiveMQConnectionFactory("tcp://localhost:61619");
try {
connection=factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
System.out.println("session started");
destination=session.createQueue("Parul");
consumer = session.createConsumer(destination);
System.out.println("Consumer created");
while (true) {
System.out.println("About to read");
Message msg = consumer.receive(5000);
if (msg instanceof TextMessage) {
TextMessage tm = (TextMessage) msg;
System.out.println(tm.getText());
}
else{
System.out.println("Queue Empty");
connection.stop();
break;
}
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Reciever reciever = new Reciever();
reciever.recieveMessage();
}
}
But my consumer is not able to read message from queue. What is the issue. Can you help me out in this.I want to read all 4 messsages at same time.
i am implementing the onMessgae () method of MessageListner Class but its not working even i am putting the debug point inside this method but debugger also not goes inside this method.
Use this method to receive the message from Queue.
Below is my code for receiving the message but its not giving any message when i am sending the message through sender class and its message goes to JMS Server and shows like this way on server
Name Server DestiCurrent MessagesCurrent MessagesPending MessagesReceived JMSServer-0 AdminServer 3 15 1 2
Code for Reciver:
{
import java.util.Hashtable;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class QueueReceive implements MessageListener {
public final static String Server="t3://localhost:7001";
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
public final static String JMS_FACTORY = "CF1";
enter code here
public final static String QUEUE = "Queue1";
private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueReceiver qreceiver;
private Queue queue;
private boolean quit = false;
String s1 = "avanish";
public void init(Context ctx, String queueName) throws NamingException, JMSException {
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qreceiver = qsession.createReceiver(queue);
qreceiver.setMessageListener(this);
qcon.start();
System.out.println(qreceiver.receive());
}
public void close() throws JMSException {
qreceiver.close();
qsession.close();
qcon.close();
}
public static void main(String[] args) throws Exception {
/*if (args.length != 1) {
System.out.println("Usage: java examples.jms.queue.QueueReceive WebLogicURL");
return;
}*/
InitialContext ic = getInitialContext();
QueueReceive qr = new QueueReceive();
qr.init(ic, QUEUE);
System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");
synchronized (qr) {
while (!qr.quit) {
try {
qr.wait();
} catch (InterruptedException ie) {
}
}
}
qr.close();
}
private static InitialContext getInitialContext() throws NamingException {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, Server);
return new InitialContext(env);
}
#Override
public void onMessage(Message msg) {
try {
String msgText;
if(msg instanceof TextMessage){
msgText = ((TextMessage) msg).getText();
}
else{
msgText = msg.toString();
}
System.out.println("reciver msg" + msgText);
if(msgText.equalsIgnoreCase("quit")){
synchronized (this) {
quit= true;
this.notifyAll();
}
}
} catch (JMSException e) {
System.err.println("Exception" + e.getMessage());
}
}
}
}
Code for Sender:
{
import javax.jms.JMSException;
import javax.jms.Queue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import javax.jms.*;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class QueueSend {
public final static String Server="t3://localhost:7001";
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
public final static String JMS_FACTORY = "CF1";
public final static String QUEUE = "Queue1";
// public final static String QUEUE = "DQ";
private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueSender qsender;
private Queue queue;
private TextMessage msg;
public void init(Context ctx, String queueName) throws NamingException, JMSException
{
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();
}
public void send(String message) throws JMSException {
msg.setText(message);
//msg.acknowledge();
qsender.setPriority(9);
qsender.send(msg);
}
public void close() throws JMSException {
qsender.close();
qsession.close();
qcon.close();
}
public static void main(String[] args) throws Exception {
InitialContext ic = getInitialContext();
QueueSend qs = new QueueSend();
qs.init(ic, QUEUE);
readAndSend(qs);
qs.close();
}
private static void readAndSend(QueueSend qs) throws IOException, JMSException
{
BufferedReader msgStream = new BufferedReader(new InputStreamReader(System.in));
String line = null;
boolean quitNow = false;
do {
System.out.print("Enter message (\"quit\" to quit): \n");
line = msgStream.readLine();
if (line != null && line.trim().length() != 0) {
qs.send(line);
System.out.println("JMS Message Sent: " + line + "\n");
quitNow = line.equalsIgnoreCase("quit");
}
} while (!quitNow);
}
private static InitialContext getInitialContext()
throws NamingException
{
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, Server);
return new InitialContext(env);
}
}
}
Insted of using MessageListener i used below lines of code and getting the exact message from queue
queue = (Queue) ctx.lookup(queueName);
qreceiver = qsession.createReceiver(queue);
message = qreceiver.receiveNoWait();
its successfully received the messages from Queue.
I've tested JMS messages sending serially and concurrently(5 threads send jms messages concurrently from the producer).
When I send 100 messages concurrently, few messages payload at the receiving end are NULL. When sent serially there is no issue.
Do I need to setup a session pool or use MDB at the consumer side to handle the messages concurrently? The setup of the JMS is good, because we are receiving messages. Am I missing anything here?
Short description of the pproject setup:
Publisher is a stateless session bean
Weblogic 8.1 jms server connection factory and destination are retrieved through
JNDI
Consumer is a java class which subscribes to this server JMS queue
and performs the tasks. (this is not a MDB or a Threaded class, listens to the queue
asynchronously)
EDITED
JmsConsumer
package net;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Hashtable;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
public class ReadJMS implements MessageListener, ExceptionListener {
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
public final static String PROVIDER_URL = "t3://address:7003";
public final static String JMS_FACTORY = "MSS.QueueConnectionFactory";
public final static String QUEUE = "jms.queue";
#SuppressWarnings("null")
public void receiveMessage() throws Exception {
// System.out.println("receiveMessage()..");
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
// Define queue
QueueReceiver qreceiver = null;
QueueSession qsession = null;
QueueConnection qcon = null;
ReadJMS async = new ReadJMS();
try {
InitialContext ctx = new InitialContext(env);
QueueConnectionFactory qconFactory = (QueueConnectionFactory) ctx
.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qcon.setExceptionListener(async);
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) ctx.lookup(QUEUE);
qreceiver = qsession.createReceiver(queue);
qreceiver.setMessageListener(async);
qcon.start();
System.out.println("readingMessage()..");
// TextMessage msg = (TextMessage) qreceiver.receive();
// System.out.println("Message read from " + QUEUE + " : "
// + msg.getText());
// msg.acknowledge();
} catch (Exception ex) {
ex.printStackTrace();
}
// } finally {
// if (qreceiver != null)
// qreceiver.close();
// if (qsession != null)
// qsession.close();
// if (qcon != null)
// qcon.close();
// }
}
public static void main(String[] args) throws Exception {
ReadJMS test = new ReadJMS();
System.out.println("init");
test.receiveMessage();
while (true) {
Thread.sleep(10000);
}
}
public void onException(JMSException arg0) {
System.err.println("Exception: " + arg0.getLocalizedMessage());
}
public synchronized void onMessage(Message arg0) {
try {
if(((TextMessage)arg0).getText() == null || ((TextMessage)arg0).getText().trim().length()==0){
System.out.println(" " + QUEUE + " : "
+ ((TextMessage) arg0).getText());
}
System.out.print(".");
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter("Output.txt", true)));
Date now = new Date();
out.println("message: "+now.toString()+ " - "+((TextMessage)arg0).getText()+"");
out.close();
} catch (JMSException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
WriteJms
package net;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Hashtable;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
public class WriteJMS {
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
public final static String PROVIDER_URL = "t3://url:7003";
public final static String JMS_FACTORY = "MSS.QueueConnectionFactory";
public final static String QUEUE = "jms.queue";
#SuppressWarnings("unchecked")
public void sendMessage() throws Exception {
#SuppressWarnings("rawtypes")
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
// Define queue
QueueSender qsender = null;
QueueSession qsession = null;
QueueConnection qcon = null;
try {
InitialContext ctx = new InitialContext(env);
QueueConnectionFactory qconFactory = (QueueConnectionFactory) ctx
.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) ctx.lookup(QUEUE);
TextMessage msg = qsession.createTextMessage();
msg.setText("<eventMessage><eventId>123</eventId><eventName>123</eventName><documentNumber>123</documentNumber><customerId>123</customerId><actDDTaskDate>123</actDDTaskDate><taskStatusErrorMessage>123</taskStatusErrorMessage></eventMessage>");
qsender = qsession.createSender(queue);
qsender.send(msg);
System.out.println("Message [" + msg.getText()
+ "] sent to Queue: " + QUEUE);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (qsender != null)
qsender.close();
if (qsession != null)
qsession.close();
if (qcon != null)
qcon.close();
}
}
}
Reusing a session across multiple threads is notoriously forbidden. But you don't do this. You create everything (connection session and producer) anew for each message. That is inefficient, but not incorrect and should not cause these errors. The code you give us looks good to me.
I am a little surprised that no exceptions occur at the sending side. Can you give some more details about the JMS implementation? Perhaps there's more information in the message broker's log?
Have you counted the messages and does the number received equal the amount sent? Could someone else be sending messages to the same queue?