使用ibm MQ 在JAVA类中报: NoClassDefFoundError:comibmmqheadersinternaltraceNames

Python027

使用ibm MQ 在JAVA类中报: NoClassDefFoundError:comibmmqheadersinternaltraceNames,第1张

1.相关jar包:

com.ibm.mq.commonservices.jar

com.ibm.mq.headers.jar

com.ibm.mq.jar

com.ibm.mq.jmqi.jar

connector.jar

junit-4.4.jar

相关jar可以在ibm mq的安装目录下找到:

2.在mq服务器上创建相应的队列管理器,和队列,以及服务器传输通道。附上操作命令:

--启动队列管理器

strmqm  QMEMBFE

--启动监听

runmqlsr -m QMEMBFE -p 1414 -t tcp &

--用户交互管理界面程序

runmqsc  QMEMBFE

--创建本地队列

DEFINE QLOCAL (Q1)

--创建名称为DC.SVRCONN的服务器连接通道

DEFINE CHANNEL(DC.SVRCONN) CHLTYPE (SVRCONN) REPLACE

--删除通道

DELETE CHANNEL(DC.SVRCONN)

3.使用java编写调用程序:

运行方法:单元测试运行:testMQ

MessageByMQ

[java] view plain copy

import org.junit.After

import org.junit.Before

import org.junit.Test

import com.ibm.mq.MQC

import com.ibm.mq.MQEnvironment

import com.ibm.mq.MQException

import com.ibm.mq.MQGetMessageOptions

import com.ibm.mq.MQMessage

import com.ibm.mq.MQPutMessageOptions

import com.ibm.mq.MQQueue

import com.ibm.mq.MQQueueManager

/**

* 列出常用的错误码:如下:

* 2540:通道定义有错误:</p>

*      解决方式如下:</p>

*          1.进入用户交互界面:runmqsc  QMEMBFE(QMEMBFE为队列管理器名称)

*          2.创建相应的服务器连接通道:DEFINE CHANNEL(DC.SVRCONN) CHLTYPE (SVRCONN) REPLACE

* 2035:授权相关错误:解决方式如下:

*      进入用户交互界面:(同上):

*  ALTER CHANNEL(DC.SVRCONN) CHLTYPE(SVRCONN) MCAUSER('mqm')

*  说明:DC.SVRCONN 服务器连接通道名称 mqm为ibm mq用户名称

*

*

*

*

* @author kefan

*

*/

public class MessageByMQ {

/**

* 队列管理器的名称

*/

private String qManagerName="QMEMBFE"

/**

* 队列管理器

*/

private   MQQueueManager qMgr

/**

* 队列名称

*/

private  String queueName="Q1"

/**

* 队列

*/

private MQQueue qQueue

/**

* mq服务器所在的主机名称

*/

private String hostname="192.168.233.134"

/**

* 服务器连接通道名称

*/

private String channelName="DC.SVRCONN"

/**

* 监听器监听的端口

*/

private  int  port=1414

/**

* 传输的编码类型

*/

private  int CCSID = 1381

@Before

public  void  init(){

try {

MQEnvironment.hostname = this.hostname // 安装MQ所在的ip address

MQEnvironment.port = this.port // TCP/IP port

MQEnvironment.channel = this.channelName

MQEnvironment.CCSID = CCSID

qMgr = new MQQueueManager(this.qManagerName)

int qOptioin = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_INQUIRE

| MQC.MQOO_OUTPUT

qQueue = qMgr.accessQueue(queueName, qOptioin)

} catch (MQException e) {

e.printStackTrace()

}

}

/**

* 发送信息

*/

public void SendMsg(byte[] qByte) {

try {

MQMessage qMsg = new MQMessage()

qMsg.write(qByte)

MQPutMessageOptions pmo = new MQPutMessageOptions()

qQueue.put(qMsg, pmo)

System.out.println("The message is sent!")

System.out.println("\tThe message is " + new String(qByte, "GBK"))

} catch (MQException e) {

e.printStackTrace()

System.out

.println("A WebSphere MQ error occurred : Completion code "

+ e.completionCode + " Reason Code is "

+ e.reasonCode)

} catch (java.io.IOException e) {

e.printStackTrace()

System.out

.println("An error occurred whilst to the message buffer "

+ e)

}

}

/**

* 从消息队列取数据

*/

public void GetMsg() {

try {

MQMessage retrievedMessage = new MQMessage()

MQGetMessageOptions gmo = new MQGetMessageOptions()

gmo.options += MQC.MQPMO_SYNCPOINT

qQueue.get(retrievedMessage, gmo)

int length = retrievedMessage.getDataLength()

byte[] msg = new byte[length]

retrievedMessage.readFully(msg)

String sMsg = new String(msg,"GBK")

System.out.println(sMsg)

} catch (RuntimeException e) {

e.printStackTrace()

} catch (MQException e) {

e.printStackTrace()

if (e.reasonCode != 2033) // 没有消息

{

e.printStackTrace()

System.out

.println("A WebSphere MQ error occurred : Completion code "

+ e.completionCode

+ " Reason Code is "

+ e.reasonCode)

}

} catch (java.io.IOException e) {

System.out

.println("An error occurred whilst to the message buffer "

+ e)

}

}

/**

* 单元测试方法

*/

@Test

public  void  testMQ(){

MessageByMQ mqst = new MessageByMQ()

mqst.init()

try {

mqst.SendMsg("你好,Webshpere MQ 7.5!".getBytes("GBK"))

mqst.GetMsg()

} catch (Exception e) {

e.printStackTrace()

}

}

/**

* 释放资源

*/

@After

public  void  release(){

try {

qQueue.close()

qMgr.disconnect()

} catch (MQException e) {

System.out

.println("A WebSphere MQ error occurred : Completion code "

+ e.completionCode + " Reason Code is "

+ e.reasonCode)

}

}

}

websphere mq : 用于传输信息 具有跨平台的功能。

1 安装websphere mq 并启动

2 websphere mq 建立 queue Manager (如:MQSI_SAMPLE_QM)

3 建立queue 类型选择 Local类型 的 (如lq )

3 建立channels 类型选择Server Connection (如BridgeChannel)

java 代码如下:

package test.mq

import com.ibm.mq.*

/*

* 成功的访问mq 的java 类

*/

public class FirstMqTest {

//public static void main(String[] args[]){

//FirstMqTest first = new FirstMqTest()

//first.test()

//}

public static void main(String args[]){

FirstMqTest first = new FirstMqTest()

first.test()

}

public void test(){

String qManager = "MQSI_SAMPLE_QM"//QueueManager name

String qName = "lq"//Queue Name

try {

//configure connection parameters

MQEnvironment.hostname="172.16.17.123"//MQ Server name or IP

//MQEnvironment.port=1414//listenr port

MQEnvironment.channel="BridgeChannel"//Server-Connection Channel

MQEnvironment.CCSID =1381

// Create a connection to the QueueManager

System.out.println("Connecting to queue manager: "+qManager)

MQQueueManager qMgr = new MQQueueManager(qManager)

// Set up the options on the queue we wish to open

int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT

// Now specify the queue that we wish to open and the open options

System.out.println("Accessing queue: "+qName)

MQQueue queue = qMgr.accessQueue(qName, openOptions)

// Define a simple WebSphere MQ Message ...

MQMessage msg = new MQMessage()

// ... and write some text in UTF8 format

msg.writeUTF("Hello, World!")

// Specify the default put message options

MQPutMessageOptions pmo = new MQPutMessageOptions()

// Put the message to the queue

System.out.println("Sending a message...")

/*

* 在此测试一下 mq 的传输次列

*

*/

for(int j=0j<5j++){

String str ="test11111111111"

str = str+j

msg.writeUTF(str)

queue.put(msg, pmo)

}

queue.put(msg, pmo)

// Now get the message back again. First define a WebSphere MQ message

// to receive the data

MQMessage rcvMessage = new MQMessage()

// Specify default get message options

MQGetMessageOptions gmo = new MQGetMessageOptions()

// Get the message off the queue.

System.out.println("...and getting the message back again")

queue.get(rcvMessage, gmo)

// And display the message text...

String msgText = rcvMessage.readUTF()

System.out.println("The message is: " + msgText)

// Close the queue

System.out.println("Closing the queue")

queue.close()

// Disconnect from the QueueManager

System.out.println("Disconnecting from the Queue Manager")

qMgr.disconnect()

System.out.println("Done!")

}

catch (MQException ex) {

System.out.println("A WebSphere MQ Error occured : Completion Code "

+ ex.completionCode + " Reason Code " + ex.reasonCode)

}

catch (java.io.IOException ex) {

System.out.println("An IOException occured whilst writing to the message buffer: "

+ ex)

}

}

}