Total members 11894 |It is currently Fri Nov 22, 2024 10:40 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





JMS Message Consumer Example, read using JNDI name, timeout setting
java code
import java.util.Properties;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


public abstract class AbstractJMSConnector {


/**
* QueueConnection.
*/
protected QueueConnection queueConnection;

/**
* Queue.
*/
protected Queue queue;
/**
* InitialContext.
*/
protected InitialContext context;
/**
* QueueSession.
*/
protected QueueSession session;

/**
* QueueConnectionFactory.
*/
protected QueueConnectionFactory queueConnectionFactory;

/**
* Use default initial context to initialize JMS utility.
*
* @param queueJindiName
* the jindi name of the queue to operate on
* @param connectionFactoryJindiName
* jindi name of connection factory
*/
protected AbstractJMSConnector(String queueJindiName,
String connectionFactoryJindiName) {

Object[] params = { queueJindiName, connectionFactoryJindiName };

try {
context = new InitialContext();

} catch (NamingException cause) {
//
}

initialzeSession(queueJindiName, connectionFactoryJindiName, null, null);

}

/**
* @param contextFactory
* to initialize the InitialContext
* @param providerURl
* to initialize the InitialContext
* @param queueJindiName
* the jindi name of the queue to operate on
* @param connectionFactoryJindiName
* jindi name of connection factory
* @param userName
* of the server
* @param password
* of the server
*/
protected AbstractJMSConnector(String contextFactory, String providerURl,
String queueJindiName, String connectionFactoryJindiName,
String userName, String password) {





try {
Properties properties = new Properties();

properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
contextFactory);
properties.setProperty(Context.PROVIDER_URL, providerURl);
properties.setProperty(Context.SECURITY_PRINCIPAL, userName);
properties.setProperty(Context.SECURITY_CREDENTIALS, password);

context = new InitialContext(properties);

} catch (NamingException cause) {
}

initialzeSession(queueJindiName, connectionFactoryJindiName, userName,
password);

}

private void initialzeSession(String queueJindiName,
String connectionFactoryJindiName, String userName, String password) {

try {
queueConnectionFactory = (QueueConnectionFactory) context
.lookup(connectionFactoryJindiName);
} catch (NamingException cause) {

}

try {
queue = (Queue) context.lookup(queueJindiName);
} catch (NamingException cause) {
}

try {
if (userName!=null && password!=null) {
queueConnection = queueConnectionFactory.createQueueConnection(
userName, password);
} else {
queueConnection = queueConnectionFactory
.createQueueConnection();
}
} catch (JMSException exception) {
//
}

try {
session = queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
} catch (JMSException exception) {
}
}

/**
* Release the resources.
*/
public void release() {


try {
if(session != null) {
session.close();
}
} catch (JMSException exception) {
//
}
try {
if(queueConnection != null) {
queueConnection.close();
}
} catch (JMSException exception) {
//
}

}

}


java code
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Queue;

public class JMSMessageConsumer extends AbstractJMSConnector {

private MessageConsumer messageConsumer;
private String messageSelecetor;

/**
* @return the messageSelecetor
*/
public String getMessageSelecetor() {
return messageSelecetor;
}

/**
* @param messageSelecetor
* the messageSelecetor to set
*/
public void setMessageSelecetor(String messageSelecetor) {
this.messageSelecetor = messageSelecetor;
}

private void initializeConsumer() {
Object[] params = {};

try {

messageConsumer = session.createConsumer(queue);
queueConnection.start();

} catch (JMSException exception) {

}

try {
messageConsumer = session.createConsumer(queue);
} catch (JMSException jmsException) {

}

}

/**
* @param contextFactory
* context factory .
* @param providerURl
* Provider URL .
* @param jindiNamequeue
* @param jindiNameCF
*/

private JMSMessageConsumer(String contextFactory, String providerURl,
String jindiNamequeue, String jindiNameCF, String userName,
String password) {
super(contextFactory, providerURl, jindiNamequeue, jindiNameCF,
userName, password);


initializeConsumer();

}

private JMSMessageConsumer(String jindiNamequeue, String jindiNameCF) {
super(jindiNamequeue, jindiNameCF);


initializeConsumer();

}

/**
* @param contextFactory
* context factory .
* @param providerURl
* Provider URL .
* @param jindiNamequeue
* @param jindiNameCF
* @throws JMSBusinessException
*/

private JMSMessageConsumer(String contextFactory, String providerURl,
String jindiNamequeue, String jindiNameCF, String userName,
String password, String messageSelector) {
super(contextFactory, providerURl, jindiNamequeue, jindiNameCF,
userName, password);



initializeConsumer();

this.messageSelecetor = messageSelector;

}

private JMSMessageConsumer(String jindiNamequeue, String jindiNameCF,
String messageSelector) {
super(jindiNamequeue, jindiNameCF);


initializeConsumer();

this.messageSelecetor = messageSelector;

}

public static JMSMessageConsumer getInstance(String contextFactory,
String providerURl, String jindiNamequeue, String jindiNameCF,
String userName, String password) {

return new JMSMessageConsumer(contextFactory, providerURl,
jindiNamequeue, jindiNameCF, userName, password);
}

public static JMSMessageConsumer getInstance(String contextFactory,
String providerURl, String jindiNamequeue, String jindiNameCF,
String userName, String password, String messageSelector) {

return new JMSMessageConsumer(contextFactory, providerURl,
jindiNamequeue, jindiNameCF, userName, password,
messageSelector);
}

public static JMSMessageConsumer getInstance(String jindiNamequeue,
String jindiNameCF) {

return new JMSMessageConsumer(jindiNamequeue, jindiNameCF);
}

public static JMSMessageConsumer getInstance(String jindiNamequeue,
String jindiNameCF, String messageSelector) {

return new JMSMessageConsumer(jindiNamequeue, jindiNameCF,
messageSelector);
}

public Message receive() {
return receive(0);
}

public Message receive(long timeout) {

Message result = null;

if (messageSelecetor != null) {
try {
messageConsumer = session.createConsumer(queue,
messageSelecetor);
} catch (JMSException jmsException) {
//
}

}
try {

if (timeout != -1) {
result = messageConsumer.receive(timeout);
} else {
result = messageConsumer.receiveNoWait();
}

} catch (JMSException jmsException) {
}

if (messageSelecetor != null) {
try {
messageConsumer.close();
} catch (JMSException jmsException) {

}
}

return result;
}

public Message receiveNoWait() {
return receive(-1);
}

@Override
public void release() {
Object[] params = {};
try {
if (messageConsumer != null) {
messageConsumer.close();
}
} catch (JMSException jmsException) {
// null
} finally {
super.release();
}

}

public Queue getQueue() {
return queue;
}

public String getQueuueDescription() {
return queue.toString();
}

}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : JMS Message Consumer Example
 Java message service(JMS)     -  
 How to Show a message before the login ?     -  
 Text message board RPG     -  
 Trace soap message     -  
 Popup message without using javascript     -  
 Editing guest message in PHPbb3     -  
 stick message even when user scrolls down or up     -  
 Popup message using JavaScript without using alert function     -  
 how to write code message to user before session will expire     -  
 show loading status message until the applet is fully loaded     -  



Topic Tags

Java JMS






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com