2019年10月26日 星期六

[JAVA] IBM MQ PUT / GET Example


// need to compile with com.ibm.mq.jar and j2ee.jar

import java.io.*;
import com.ibm.mq.*;

public class MQTest
{
private static String qMngrSrtr = "";
private static String replyQueue = "";
private static String requestQueue = "";
private static String hostName = "";
private static int port = 0;
private static final String channel = "SYSTEM.AITP.SVRCONN";
private static final String FILEPATH = "C:\\Test\\";

MQQueueManager qManager;

public void init(String environment)
{
try
{
qMngrSrtr = "MBKCQ04";
replyQueue = "MFS.MBK.REPL06";
requestQueue = "MBK.MFS.REQS01";
hostName = "10.41.72.69";
port = 1544;
MQEnvironment.hostname = hostName;
MQEnvironment.channel =- channel;
MQEnvironment.port = port;
qManager = new MQQueueManager(qMngrSrtr);
}
catch(Exception e)
{
e.printStackTrace();
}
}

public void putAndGetMessage(String path, String filename)
{
try
{
int openOptions2 = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;
int openOptions3 = MQC.MQOO_INPUT_SHARED | MQC.MQOO_FAIL_IF_QUIESCING;
MQQueue mqReply = qManager.accessQueue(replyQueue, openOptions3, null, null, null);

MQQueue mqReqest = qManager.accessQueue(requestQueue, openOptions2, null, null, null);

MQMessage putMessage = new MQMessage();
putMessage.format = MQC.MQFMT_STRING;
putMessage.messageFlags = MQC.MQFMT_REQUEST;
putMessage.replyToQueueName = replyQueue;
putMessage.replyToQueueManagerName = qMngrSrtr;

String msg = readFile(path + filename);
putMessage.writeString(msg);

MQPutMessageOptions pmo = new MQPutMessageOptions();
pmo.options += MQC.MQPMO_NEW_MSG_ID | MQC.MQPMO_SYNCPOINT;
mqReqest.put(putMessage, pmo);
qManager.commit();
mqReqest.close();

MQMessage getMessages = new MQMessage();
getMessages.correlationId = putMessage.messageId;

MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.options += MQC.MQGMO_SYNCPOINT | MQC.MQGMO_WAIT;
gmo.maatchOptions = MQC.MQMO_MATCH_CORREL_ID;
gmo.waitInterval = 30000000;

mqReply.get(getMessages, gmo);
int a = getMessages.getDataLength();

String retrivedMsg = formatXmlString(getMessages.readStringOfCharLength(a));
System.out.println(retrivedMsg);
writeFile(path + filename + ".out", retrivedMsg);
mqReply.close();


}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
qManager.disconnect();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

public static String readFile(String filename)
{
BufferedReader br = null;
try
{
br = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "utf-8"));
StringBuffer sb = new StringBuffer();
String tmp = null;
while(true)
{
tmp = br.readLine();
if(tmp == null)
{
break;
}
sb.append(tmp.trim());
}
return sb.toString();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
br.close();
}
catch(Exception e)
{
}
}
return "";
}

public static void writeFile(String filename, String msg)
{
BufferedWriter br = null;
try
{
br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "utf-8"));
br.write(msg);
br.newLine();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
br.close();
}
catch(Exception e)
{
}
}
}

public static String formatXmlString(String s)
{
StringBuffer sb = new StringBuffer();
try
{
char[] arr = s.toCharArray();
boolean quoteOpenFlag = false;
int arrowOpenFlag = -1;
boolean dataFlag = false;
boolean escapeFlag = false;
boolean firstArrowFlag = true;
boolean tagOpenFlag = false;
char prevC = '\0';
for(char c:arr)
{
if(c=='<')
{
if(!quoteOpenFlag)
{
if(prevC=='>')
{
dataFlag = true;
}
}
escapeFlag = false;
}
else
{
if(!quoteOpenFlag && prevC=='<' && c!='/')
{
if(firstArrowFlag)
{
firstArrowFlag = false;
}
else
{
sb.append("\n");
}
for(int i=0;i<arrowOpenFlag;i++)
{
sb.append("\t");
}
sb.append("<");
}

if(c=='>')
{
if(!quoteOpenFlag)
{

}
sb.append(c);
escapeFlag = false;
}
else if(c=='"')
{
if(!escapeFlag)
{
quoteOpenFlag = !quoteOpenFlag;
}
sb.append(c);
escapeFlag = false;
}
else if(c=='/')
{
if(!quoteOpenFlag && prevC=='<')
{
arrowOpenFlag--;
if(tagOpenFlag)
{
tagOpenFlag = false;
sb.append(prevC);
sb.append(c);
if(!dataFlag)
{
sb.append("\n");
for(int i = 0;i<arrowOpenFlag;i++)
{
sb.append("\t");
}
}
}
else
{
sb.append("\n");
for(int i = 0;i<arrowOpenFlag;i++)
{
sb.append("\t");
}
sb.append(prevC);
sb.append(c);
}
dataFlag = false;
}
escapeFlag = false;
}
else if(c=='\\')
{
escapeFlag = true;
sb.append(c);
}
else
{
if(!quoteOpenFlag && prevC=='>')
{
dataFlag = true;
}
if(!quoteOpenFlag && prevC=='<')
{
arrowOpenFlag++;
tagOpenFlag = true;
}
sb.append(c);
escapeFlag = false;
}
}
// end if
prevC = c;
}
return sb.toString();
}
catch(Exception e)
{
e.printStackTrace();
}
return "";
}

public static void main(String[] args)
{
MQTest test = new MQTest();
test.init();
test.putAndGetMessage(FILEPATH, "in.txt");
}
}

2019年10月12日 星期六

MAC OS INSTALL php intl extension

Go to https://php-osx.liip.ch

Choose your php version (e.g 7.2)

Copy command

Open terminal

curl -s https://php-osx.liip.ch/install.sh | bash -s 7.2

// this will install php under /usr/local

cd /usr/local/php5-7.2.21-20190811-210031/lib/php/extensions/no-debug-non-zts-20170718

sudo cp intl.so /Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20170718/intl.so

Restart apache web server

Good luck