package dk.topsecurity;
//import javax.jms.*;
import java.util.Date;
import java.util.Hashtable;
import javax.jms.DeliveryMode;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.directory.InitialDirContext;
import com.ibm.mq.jms.MQQueue;
import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueSender;
import com.ibm.mq.jms.MQQueueSession;
public class ToMQnative implements ExceptionListener {
/**
* Setting up authorisation username. Used by classes in com.ibm.mqjms.jar
* Setting up queue connection factory (QCF)
* Setting up name of queue, as defined in the MQ setup
* Setting up url for file-based, external JNDI provider
* Setting up context factory to use with external JNDI
*/
public String mq_username = "Administrator";
public String mq_qcf = "TOPSECURITY_QCF";
public String mq_qname = "TOPSECURITY.QUEUE";
public String mq_url = "file:/C:/jms-jndi-directory";
public String mq_jndi = "com.sun.jndi.fscontext.RefFSContextFactory";
/**
* main method - binding to the Websphere MQ installation - and delivering a
* number of messages.
*
* Make sure you've run the MQSC commands on MQ before deploying examples
*
* define qcf(TOPSECURITY_QCF) qmgr(BEAQM) channel(SYSTEM.DEF.SVRCONN) hostname(semp20) port(1421) transport(CLIENT)
* define q(TOPSECURITY.QUEUE) queue(TOPSECURITY.QUEUE) qmgr(BEAQM)
*/
public static void main(String[] args) {
ToMQnative sender = null;
try {
sender = new ToMQnative();
sender.setupQueueConnection();
for(int i=0;;i++) {
sender.send(sender.setMessage("Hello no "+i+" from client at "+new Date()));
Thread.sleep(1000L);
}
}
catch(JMSException je) {
System.out.println("Caught JMSException: "+je);
Exception le = je.getLinkedException();
if (le != null)
System.out.println("Linked exception: "+le);
je.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if (sender != null)
sender.cleanup();
} catch (Exception e) { }
}
}
private void setupQueueConnection() throws Exception {
//set the username which to use for authentication
System.out.println("Initial user.name="+System.getProperty("user.name"));
System.setProperty("user.name",mq_username);
System.out.println("Authrorisation required user.name="+System.getProperty("user.name"));
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, mq_jndi);
env.put(Context.PROVIDER_URL, mq_url);
InitialDirContext ctx = new InitialDirContext(env);
com.ibm.mq.jms.MQQueueConnectionFactory factory =
(com.ibm.mq.jms.MQQueueConnectionFactory)ctx.lookup(mq_qcf);
System.out.println("Factory = " +factory);
/* Create a QueueConnection, QueueSession
* When a connection is made, use the createQueueSession method on the
* QueueConnection to obtain a session. Parameters:
* boolean= determines whether the session is transacted or non-transacted.
* int = that determines the acknowledge mode.
* Simplest case is that of the non-transacted session with AUTO_ACKNOWLEDGE
* - p319 in the IBM redbook.
*/
connection = (MQQueueConnection)factory.createQueueConnection();
session = (MQQueueSession)connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
ioQueue = new MQQueue();
ioQueue.setBaseQueueName(mq_qname);
ioQueue.setPersistence(DeliveryMode.PERSISTENT);
connection.start();
connection.setExceptionListener(this);
queueSender = (MQQueueSender)session.createSender(ioQueue);
}
/**
* For test purpose, create one of many possible types of messages:
* BytesMessage, MapMessage, ObjectMessage, StreamMessage
*/
TextMessage setMessage(String text) throws Exception {
TextMessage msg = session.createTextMessage();
msg.setText(text);
return(msg);
}
/**
* Send message off to MQ
*/
void send(TextMessage msg) throws Exception {
System.out.println("To " + ioQueue.getQueueName() + " sending message=" + msg.getText());
queueSender.send(msg);
}
/**
* required in the ExceptionListener interface
*/
public void onException(JMSException jms) {
System.out.println("onException: jms" + jms);
jms.printStackTrace();
}
/**
* do away with connections to MQ
*/
void cleanup() throws Exception {
if (session != null) {
session.close();
session = null;
}
if (connection != null)
connection.close();
connection = null;
}
private MQQueue ioQueue;
private MQQueueConnectionFactory factory;
private MQQueueSession session;
private MQQueueConnection connection;
private MQQueueSender queueSender;
private InitialContext ctx;
private TextMessage msg;
}