package dk.topsecurity;
import javax.jms.*;
import javax.naming.*;
import javax.naming.directory.*;
import java.util.*;
import com.ibm.mq.jms.*;
import javax.transaction.xa.Xid;
import javax.transaction.xa.XAResource;
public class TopsecurityMQXAClient 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 = "noname"; //"Administrator";
public String mq_qcf = "TOPSECURITY_XAQCF";
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.
*/
public static void main(String[] args) {
TopsecurityMQXAClient sender = null;
try {
sender = new TopsecurityMQXAClient();
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);
// env.put(Context.SECURITY_PRINCIPAL, "Administrator");
InitialDirContext ctx = new InitialDirContext(env);
com.ibm.mq.jms.MQXAQueueConnectionFactory factory =
(com.ibm.mq.jms.MQXAQueueConnectionFactory)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.
*/
xaconnection = factory.createXAQueueConnection();
xasession = xaconnection.createXAQueueSession();
QueueSession normalsession = xasession.getQueueSession();
ioQueue = (Queue)ctx.lookup(mq_qname);
queueSender = normalsession.createSender(ioQueue);
/* //we only use this when we want to receive messages transactionally
xaconnection.start();
*/
xaconnection.setExceptionListener(this);
}
/**
* For test purpose, create one of many possible types of messages:
* BytesMessage, MapMessage, ObjectMessage, StreamMessage
*/
TextMessage setMessage(String text) throws Exception {
TextMessage msg = xasession.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());
XAResource xares = xasession.getXAResource();
Xid xid = new XidImpl(102);
xares.start(xid, XAResource.TMNOFLAGS);
System.out.println("Sending the message on queue " + ioQueue.getQueueName());
try {
queueSender.send(msg);
System.out.println("sending successfull");
} catch(Exception ex) {
System.out.println("sending unsuccessfull");
System.out.println(ex);
}
xares.end(xid,XAResource.TMNOFLAGS);
//xares.prepare(xid);
xares.commit(xid, true);
System.out.println("sending completed");
}
public void onException(JMSException jms) {
System.out.println("onException: jms" + jms);
jms.printStackTrace();
}
void cleanup() throws Exception {
if (xasession != null) {
xasession.close();
xasession = null;
}
if (xaconnection != null)
xaconnection.close();
xaconnection = null;
}
private Queue ioQueue;
private XAQueueSession xasession;
private XAQueueConnection xaconnection;
private XAQueueConnectionFactory factory;
private QueueSender queueSender;
private InitialContext ctx;
private TextMessage msg;
static class XidImpl implements Xid {
private byte _branch[] = new byte[64];
private byte _global[] = new byte[64];
public XidImpl(int id) {
_branch[60] = (byte) ((id >>> 24) & 0xFF);
_branch[61] = (byte) ((id >>> 16) & 0xFF);
_branch[62] = (byte) ((id >>> 8) & 0xFF);
_branch[63] = (byte) ( id & 0xFF);
}
public byte[] getGlobalTransactionId() { return _global; }
public byte[] getBranchQualifier() { return _branch; }
public int getFormatId() { return 0; }
}
}