Simple Point-to-point application using WebSphere MQ JMS
package my.samples;
import javax.jms.JMSException;
import javax.jms.Session;
import com.ibm.jms.JMSMessage;
import com.ibm.jms.JMSTextMessage;
import com.ibm.mq.jms.JMSC;
import com.ibm.mq.jms.MQQueue;
import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueReceiver;
import com.ibm.mq.jms.MQQueueSender;
import com.ibm.mq.jms.MQQueueSession;
/**
* SimplePTP: A minimal and simple testcase for Point-to-point messaging (1.02 style).
*
* Assumes that the queue is empty before being run.
*
* Does not make use of JNDI for ConnectionFactory and/or Destination definitions.
*
* @author saket
*/
public class SimplePTP {
/**
* Main method
*
* @param args
*/
public static void main(String[] args) {
try {
MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
// Config
cf.setHostName("localhost");
cf.setPort(1414);
cf.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
cf.setQueueManager("QM_thinkpad");
cf.setChannel("SYSTEM.DEF.SVRCONN");
MQQueueConnection connection = (MQQueueConnection) cf.createQueueConnection();
MQQueueSession session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
MQQueue queue = (MQQueue) session.createQueue("queue:///Q1");
MQQueueSender sender = (MQQueueSender) session.createSender(queue);
MQQueueReceiver receiver = (MQQueueReceiver) session.createReceiver(queue);
long uniqueNumber = System.currentTimeMillis() % 1000;
JMSTextMessage message = (JMSTextMessage) session.createTextMessage("SimplePTP "+ uniqueNumber);
// Start the connection
connection.start();
sender.send(message);
System.out.println("Sent message:\\n" + message);
JMSMessage receivedMessage = (JMSMessage) receiver.receive(10000);
System.out.println("\\nReceived message:\\n" + receivedMessage);
sender.close();
receiver.close();
session.close();
connection.close();
System.out.println("\\nSUCCESS\\n");
}
catch (JMSException jmsex) {
System.out.println(jmsex);
System.out.println("\\nFAILURE\\n");
}
catch (Exception ex) {
System.out.println(ex);
System.out.println("\\nFAILURE\\n");
}
}
}
Simple Publish/Subscribe application using WebSphere MQ JMS
package my.samples;
import javax.jms.JMSException;
import javax.jms.Session;
import com.ibm.jms.JMSMessage;
import com.ibm.jms.JMSTextMessage;
import com.ibm.mq.jms.JMSC;
import com.ibm.mq.jms.MQTopic;
import com.ibm.mq.jms.MQTopicConnection;
import com.ibm.mq.jms.MQTopicConnectionFactory;
import com.ibm.mq.jms.MQTopicPublisher;
import com.ibm.mq.jms.MQTopicSession;
import com.ibm.mq.jms.MQTopicSubscriber;
/**
* SimplePubSub: A minimal and simple testcase for Publish/Subscribe (1.02 style).
*
* Topics are dynamically created on the queue manager and need not be pre-defined.
*
* (The Broker must be enabled on the queue manager and the JMS Publish/Subscribe
* queues must be defined manually.)
*
* Note: These samples are for WMQ Base Broker and incomplete for WebSphere Message Broker)
*
* Does not make use of JNDI for ConnectionFactory and/or Destination definitions.
*
* @author saket
*/
public class SimplePubSub {
/**
* Main method.
*
* @param args
*/
public static void main(String[] args) {
try {
MQTopicConnectionFactory cf = new MQTopicConnectionFactory();
// Config
cf.setHostName("localhost");
cf.setPort(1414);
cf.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
cf.setQueueManager("QM_thinkpad");
cf.setChannel("SYSTEM.DEF.SVRCONN");
MQTopicConnection connection = (MQTopicConnection) cf.createTopicConnection();
MQTopicSession session = (MQTopicSession) connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
MQTopic topic = (MQTopic) session.createTopic("topic://foo");
MQTopicPublisher publisher = (MQTopicPublisher) session.createPublisher(topic);
MQTopicSubscriber subscriber = (MQTopicSubscriber) session.createSubscriber(topic);
long uniqueNumber = System.currentTimeMillis() % 1000;
JMSTextMessage message = (JMSTextMessage) session.createTextMessage("SimplePubSub "+ uniqueNumber);
// Start the connection
connection.start();
publisher.publish(message);
System.out.println("Sent message:\\n" + message);
JMSMessage receivedMessage = (JMSMessage) subscriber.receive(10000);
System.out.println("\\nReceived message:\\n" + receivedMessage);
publisher.close();
subscriber.close();
session.close();
connection.close();
System.out.println("\\nSUCCESS\\n");
}
catch (JMSException jmsex) {
System.out.println(jmsex);
System.out.println("\\nFAILURE\\n");
}
catch (Exception ex) {
System.out.println(ex);
System.out.println("\\nFAILURE\\n");
}
}
}
Example P8 Java code
private void getDocumentObjectFromFileNet() throws ERDocumentDeletedException{
//A new ObjectStore object is created from the CE Connection object
ObjectStore os = null;
if (DocumentAndImageStoreDAO.currentObjectStore == null){
os = Connection.fetchOS(this.getObjectStoreName());
DocumentAndImageStoreDAO.currentObjectStore = os;
}
else {
os = DocumentAndImageStoreDAO.currentObjectStore;
}
//Retrieve the Document
Document doc = CEUtility.fetchDocById(os,this.DocumentID);
//Check if the document is deleted
try {
if (doc.getProperties().getBooleanValue("status")){
this.operationStatus = "Document Deleted";
throw new ERDocumentDeletedException("Document Deleted");
}
}catch (Exception e){
//Catch nullpointer
}
//Check the Version matches the one required
if (this.Version.equalsIgnoreCase(doc.get_MajorVersionNumber().toString() + "." + doc.get_MinorVersionNumber().toString())){
}else {
this.operationStatus = "Mismatch Version: Version Found : " + doc.get_MajorVersionNumber().toString() + "." + doc.get_MinorVersionNumber().toString() + " But Looking for Version : " + this.Version + " for Doc ID : " + this.DocumentID + " !";
return;
}
//Get the Document Content
InputStream is = doc.accessContentStream(0);
try {
if (is == null) {
this.operationStatus = "InputStream is null!";
return;
}
is = doc.accessContentStream(0);
this.contentSize = doc.get_ContentSize().intValue();
this.Content = new byte[this.contentSize];
int checkSize = is.read(this.Content);
is.close();
//Check size is right
if (checkSize != this.contentSize){
this.operationStatus = "Size of Document mismatch: Initial Size :"+ this.contentSize + "Read Size : " + checkSize;
return;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.operationStatus = "Completed";
}
package com.ibm.filenet.p8.ce.sample;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import com.filenet.api.collection.AccessPermissionList;
import com.filenet.api.collection.ContentElementList;
import com.filenet.api.collection.EngineCollection;
import com.filenet.api.collection.RepositoryRowSet;
import com.filenet.api.constants.AutoClassify;
import com.filenet.api.constants.AutoUniqueName;
import com.filenet.api.constants.CheckinType;
import com.filenet.api.constants.ComponentRelationshipType;
import com.filenet.api.constants.CompoundDocumentState;
import com.filenet.api.constants.DefineSecurityParentage;
import com.filenet.api.constants.RefreshMode;
import com.filenet.api.constants.VersionBindType;
import com.filenet.api.core.ComponentRelationship;
import com.filenet.api.core.Containable;
import com.filenet.api.core.ContentTransfer;
import com.filenet.api.core.CustomObject;
import com.filenet.api.core.Document;
import com.filenet.api.core.Factory;
import com.filenet.api.core.Folder;
import com.filenet.api.core.ObjectStore;
import com.filenet.api.core.ReferentialContainmentRelationship;
import com.filenet.api.property.Properties;
import com.filenet.api.property.Property;
import com.filenet.api.query.RepositoryRow;
import com.filenet.api.query.SearchSQL;
import com.filenet.api.query.SearchScope;
import com.filenet.api.security.AccessPermission;
import com.filenet.api.util.Id;
/**
* Provides the static methods for making API
* calls to Content Engine.
*/
public class CEUtil
{
/*
* Creates the file using bytes read from Document's
* content stream.
*/
public static void writeDocContentToFile(Document doc, String path)
{
String fileName = doc.get_Name();
File f = new File(path,fileName);
InputStream is = doc.accessContentStream(0);
int c = 0;
try
{
FileOutputStream out = new FileOutputStream(f);
c = is.read();
while ( c != -1)
{
out.write(c);
c = is.read();
}
is.close();
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
/*
* Reads the content from a file and stores it
* in a byte array. The byte array will later be
* used to create ContentTransfer object.
*/
public static byte[] readDocContentFromFile(File f)
{
FileInputStream is;
byte[] b = null;
int fileLength = (int)f.length();
if(fileLength != 0)
{
try
{
is = new FileInputStream(f);
b = new byte[fileLength];
is.read(b);
is.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return b;
}
/*
* Creates the ContentTransfer object from supplied file's
* content.
*/
public static ContentTransfer createContentTransfer(File f)
{
ContentTransfer ctNew = null;
if(readDocContentFromFile(f) != null)
{
ctNew = Factory.ContentTransfer.createInstance();
ByteArrayInputStream is = new ByteArrayInputStream(readDocContentFromFile(f));
ctNew.setCaptureSource(is);
ctNew.set_RetrievalName(f.getName());
}
return ctNew;
}
/*
* Creates the ContentElementList from ContentTransfer object.
*/
public static ContentElementList createContentElements(File f)
{
ContentElementList cel = null;
if(createContentTransfer(f) != null)
{
cel = Factory.ContentElement.createList();
ContentTransfer ctNew = createContentTransfer(f);
cel.add(ctNew);
}
return cel;
}
/*
* Creates the Document with content from supplied file.
*/
public static Document createDocWithContent(File f, String mimeType, ObjectStore os, String docName, String docClass)
{
Document doc = null;
if (docClass.equals(""))
doc = Factory.Document.createInstance(os, null);
else
doc = Factory.Document.createInstance(os, docClass);
doc.getProperties().putValue("DocumentTitle", docName);
doc.set_MimeType(mimeType);
ContentElementList cel = CEUtil.createContentElements(f);
if (cel != null)
doc.set_ContentElements(cel);
return doc;
}
/*
* Creates the Document without content.
*/
public static Document createDocNoContent(String mimeType, ObjectStore os, String docName, String docClass)
{
Document doc = null;
if (docClass.equals(""))
doc = Factory.Document.createInstance(os, null);
else
doc = Factory.Document.createInstance(os, docClass);
doc.getProperties().putValue("DocumentTitle", docName);
doc.set_MimeType(mimeType);
return doc;
}
/*
* Retrieves the Document object specified by path.
*/
public static Document fetchDocByPath(ObjectStore os, String path)
{
Document doc = Factory.Document.fetchInstance(os, path, null);
return doc;
}
/*
* Retrieves the Document object specified by id.
*/
public static Document fetchDocById(ObjectStore os, String id)
{
Id id1 = new Id(id);
Document doc = Factory.Document.fetchInstance(os, id1, null);
return doc;
}
/*
* Checks in the Document object.
*/
public static void checkinDoc(Document doc)
{
doc.checkin(AutoClassify.AUTO_CLASSIFY, CheckinType.MINOR_VERSION);
doc.save(RefreshMode.REFRESH);
doc.refresh();
}
/*
* Creates the CustomObject.
*/
public static CustomObject createCustomObject(ObjectStore os, String className)
{
CustomObject co = null;
if (className.equals(""))
co = Factory.CustomObject.createInstance(os,null);
else
co = Factory.CustomObject.createInstance(os, className);
return co;
}
/*
* Retrieves the CustomObject object specified by path.
*/
public static CustomObject fetchCustomObjectByPath(ObjectStore os, String path)
{
CustomObject doc = Factory.CustomObject.fetchInstance(os,path,null);
return doc;
}
/*
* Retrieves the CustomObject object specified by id.
*/
public static CustomObject fetchCustomObjectById(ObjectStore os, String id)
{
Id id1 = new Id(id);
CustomObject doc = Factory.CustomObject.fetchInstance(os,id1,null);
return doc;
}
/*
* Files the Containable object (i.e. Document, CustomObject) in
* specified folder.
*/
public static ReferentialContainmentRelationship fileObject(ObjectStore os, Containable o, String folderPath)
{
Folder fo = Factory.Folder.fetchInstance(os,folderPath,null);
ReferentialContainmentRelationship rcr;
if (o instanceof Document)
rcr = fo.file((Document) o, AutoUniqueName.AUTO_UNIQUE,
((Document) o).get_Name(), DefineSecurityParentage.DO_NOT_DEFINE_SECURITY_PARENTAGE);
else
rcr = fo.file((CustomObject) o, AutoUniqueName.AUTO_UNIQUE,
((CustomObject) o).get_Name(), DefineSecurityParentage.DO_NOT_DEFINE_SECURITY_PARENTAGE);
return rcr;
}
/*
* Retrieves some of the properties of Containable object
* (i.e. Document, CustomObject) and stores them in a HashMap,
* with property name as key and property value as value.
*/
public static HashMap getContainableObjectProperties(Containable o)
{
HashMap hm = new HashMap();
hm.put("Id", o.get_Id().toString());
hm.put("Name", o.get_Name());
hm.put("Creator", o.get_Creator());
hm.put("Owner", o.get_Owner());
hm.put("Date Created",o.get_DateCreated().toString());
hm.put("Date Last Modified", o.get_DateLastModified().toString());
return hm;
}
/*
* Creates the Folder object at specified path using specified name.
*/
public static void createFolder(ObjectStore os, String fPath, String fName, String className)
{
Folder f = Factory.Folder.fetchInstance(os, fPath, null);
Folder nf = null;
if (className.equals(""))
nf = Factory.Folder.createInstance(os, null);
else
nf = Factory.Folder.createInstance(os, className);
nf.set_Parent(f);
nf.set_FolderName(fName);
nf.save(RefreshMode.REFRESH);
}
/*
* Creates the CompoundDocument (i.e. ComponentRelationship object).
*/
public static ComponentRelationship createComponentRelationship(ObjectStore os, String pTitle, String cTitle)
{
ComponentRelationship cr = null;
Document parentDoc = null;
Document childDoc = null;
parentDoc = Factory.Document.createInstance(os, null);
parentDoc.getProperties().putValue("DocumentTitle", pTitle);
parentDoc.set_CompoundDocumentState(CompoundDocumentState.COMPOUND_DOCUMENT);
parentDoc.save(RefreshMode.REFRESH);
parentDoc.checkin(AutoClassify.AUTO_CLASSIFY, CheckinType.MINOR_VERSION);
parentDoc.save(RefreshMode.REFRESH);
childDoc = Factory.Document.createInstance(os, null);
childDoc.getProperties().putValue("DocumentTitle", cTitle);
childDoc.set_CompoundDocumentState(CompoundDocumentState.COMPOUND_DOCUMENT);
childDoc.save(RefreshMode.REFRESH);
childDoc.checkin(AutoClassify.AUTO_CLASSIFY, CheckinType.MINOR_VERSION);
childDoc.save(RefreshMode.REFRESH);
cr = Factory.ComponentRelationship.createInstance(os, null);
cr.set_ParentComponent(parentDoc);
cr.set_ChildComponent(childDoc);
cr.set_ComponentRelationshipType(ComponentRelationshipType.DYNAMIC_CR);
cr.set_VersionBindType(VersionBindType.LATEST_VERSION);
return cr;
}
/*
* Retrieves the CompoundDocument object using supplied ID.
*/
public static ComponentRelationship fetchComponentRelationship(ObjectStore os, String id)
{
Id id1 = new Id(id);
ComponentRelationship cr = Factory.ComponentRelationship.fetchInstance(os, id1, null);
return cr;
}
/*
* Retrieves some of the properties of CompoundDocument
* (i.e. ComponentRelationship object) and stores them in HashMap,
* property name as key and property value as value.
*/
public static HashMap getComponentRelationshipObjectProperties(ComponentRelationship o)
{
HashMap hm = new HashMap();
hm.put("Id", o.get_Id().toString());
hm.put("Creator", o.get_Creator());
hm.put("Date Created",o.get_DateCreated().toString());
hm.put("Date Last Modified", o.get_DateLastModified().toString());
hm.put("Child Component", o.get_ChildComponent().get_Name());
hm.put("Parent Component", o.get_ParentComponent().get_Name());
return hm;
}
/*
* Retrieves the RepositoryRowSet (result of querying Content Engine).
* Query is constructed from supplied select, from, and where clause using
* SearchSQL object. Then it creates the SearchScope object using supplied
* ObjectStore, and queries the Content Engine using fetchRows method
* of SearchScope object.
*/
public static RepositoryRowSet fetchResultsRowSet(ObjectStore os, String select,
String from, String where, int rows)
{
RepositoryRowSet rrs = null;
SearchSQL q = new SearchSQL();
SearchScope ss = new SearchScope(os);
q.setSelectList(select);
q.setFromClauseInitialValue(from, null, false);
if(!where.equals(""))
{
q.setWhereClause(where);
}
if(!(rows == 0))
{
q.setMaxRecords(rows);
}
rrs = ss.fetchRows(q, null, null, null);
return rrs;
}
/*
* Gets column names to display in JTable. It takes
* RepositoryRow as argument
*/
public static Vector getResultProperties(RepositoryRow rr)
{
Vector column = new Vector();
Properties ps = rr.getProperties();
Iterator it = ps.iterator();
while(it.hasNext())
{
Property pt = (Property) it.next();
String name = pt.getPropertyName();
column.add(name);
}
return column;
}
/*
* Retrieves the properties from supplied RepositoryRow,
* stores them in vector, and returns it.
*/
public static Vector getResultRow(RepositoryRow rr)
{
Vector row = new Vector();
Properties ps = rr.getProperties();
Iterator it = ps.iterator();
while(it.hasNext())
{
Property pt = (Property) it.next();
Object value = pt.getObjectValue();
if (value == null)
{
row.add("null");
}
else if (value instanceof EngineCollection)
{
row.add("*");
}
else
{
row.add(value.toString());
}
}
return row;
}
/*
* Retrieves the access permission list for a Containable
* object, stores it in Vector, and returns it.
*/
public static Vector getPermissions(Containable co)
{
Vector permissions = new Vector();
AccessPermissionList apl = co.get_Permissions();
Iterator iter = apl.iterator();
while (iter.hasNext())
{
AccessPermission ap = (AccessPermission)iter.next();
permissions.add("GRANTEE_NAME: " + ap.get_GranteeName());
permissions.add("ACCESS_MASK: " + ap.get_AccessMask().toString());
permissions.add("ACCESS_TYPE: " + ap.get_AccessType().toString());
}
return permissions;
}
}