1.mq服务器端的配置:
1.1配置队列管理器
1.2配置服务器连接通道
1.3配置队列
1.4java客户端的用户认证配置:
权限重点补充一下:
本例中mq的版本为:7.5。
授权需要对队列管理器和队列分别授权:
查看java客户端的用户是否有连接mq的权限:
--查看连接队列管理器的权限:
dspmqaut -m QMEMBFE -t qmgr -p issuser
setmqaut -m QMEMBFE -t qmgr -p issuser +all +connect
注意connect权限,如果没有connect权限,则客户端会提示,无权限,貌似错误码为:2035.
issuser不应该为mqm组内的成员,因为,mq默认的会拦截mq组内的所有成员.
--查看连接队列的权限,如果没有权限,类似与上一步授权:
dspmqaut -m QMEMBFE -n 9071000030_4 -t queue -p issuser
setmqaut -m QMEMBFE -n 9071000030_4 -t queue -p issuser +all +connect
2.java客户端的编写:
2.1需要添加的jar:对应的jar可以在mq的安装目录下找到:
注:mq7.5中的com.ibm.mq.jmqi.jar有问题:可以使用反编译软件查看,会发现class有问题:
2.2代码实现:
2.2.1配置文件:
app.properties
jms.mbfe.inqueuename=9071000030_4
jms.mbfe.outqueuename=9071000030_6
systme.character=UTF-8
#add by kefan
jms.mq.queuemanagername=QMEMBFE
jms.mq.hostname=192.168.233.134
#jms.mq.hostname=10.14.2.183
jms.mq.port=1414
jms.mq.ccsid=819
jms.mq.channel=SYSTEM.ADMIN.SVRCONN
#jms.mq.channel=server1
#jms.mq.channel=server1
#jms.mq.username=administrator
#jms.mq.password=Sinomfroot123
jms.mq.username=issuser
#jms.mq.username=mqm
jms.mq.password=123456
2.2.2代码:
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Properties; import javax.jms.BytesMessage; import javax.jms.Connection; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import com.ibm.jms.JMSBytesMessage; import com.ibm.msg.client.jms.JmsConnectionFactory; import com.ibm.msg.client.jms.JmsConstants; import com.ibm.msg.client.jms.JmsFactoryFactory; import com.ibm.msg.client.wmq.WMQConstants; public class ConnectionTest { public static final String SYSTEM_CHARCTER = "UTF-8"; private Connection jmsConnection = null; private Session jmsSession = null; private Queue jmsQueue = null; private String strQuereName = "测试队列管理器"; private MessageProducer jmsProducer = null; private MessageConsumer messageConsumer = null; /** * 初始化连接参数 * * @throws JMSException */ public void initial() throws JMSException { Properties pro = new Properties(); try { pro.load(Main.class.getResourceAsStream("app.properties")); JmsFactoryFactory jmsff = JmsFactoryFactory .getInstance(JmsConstants.WMQ_PROVIDER); JmsConnectionFactory jmscf = jmsff.createConnectionFactory(); /** * 队列管理器 */ jmscf.setStringProperty("XMSC_WMQ_QUEUE_MANAGER", pro.getProperty("jms.mq.queuemanagername")); /** * 运行队列管理器的主机地址 */ jmscf.setStringProperty("XMSC_WMQ_HOST_NAME", pro.getProperty("jms.mq.hostname")); /* * 队列管理器监听的端口 */ jmscf.setIntProperty("XMSC_WMQ_PORT", Integer.parseInt((String) pro.get("jms.mq.port"))); /* * 服务器连接通道名称 */ jmscf.setStringProperty("XMSC_WMQ_CHANNEL", pro.getProperty("jms.mq.channel")); /* * 编码规则 */ jmscf.setIntProperty("CCSID", 1381); /* * 以客户端连接的方式连接队列管理器 */ jmscf.setIntProperty("XMSC_WMQ_CONNECTION_MODE", WMQConstants.WMQ_CM_CLIENT); /* * 创建连接需要制定用户名和密码:服务器端的设置需要注意:对应的用户是否有访问队列管理器和队列的权限 */ this.jmsConnection = jmscf.createConnection("issuser", "199404"); this.jmsSession = this.jmsConnection.createSession(true, 0); this.jmsQueue = this.jmsSession.createQueue(pro .getProperty("jms.mbfe.inqueuename")); this.strQuereName = "普通发送队列"; this.jmsProducer = this.jmsSession.createProducer(this.jmsQueue); messageConsumer = this.jmsSession.createConsumer(this.jmsQueue); } catch (IOException e) { e.printStackTrace(); } } /** * 发送消息 * * @param textMessage * @throws ECDSServiceException */ public void send(String textMessage) { try { BytesMessage bm = this.jmsSession.createBytesMessage(); bm.writeBytes(textMessage.getBytes(SYSTEM_CHARCTER)); this.jmsConnection.start(); this.jmsProducer.send(bm); this.jmsSession.commit(); this.close(); } catch (JMSException e) { e.getLinkedException().printStackTrace(); e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } /** * 发送消息 * * @param textMessage * @throws UnsupportedEncodingException * @throws ECDSServiceException */ public void get() throws UnsupportedEncodingException { try { this.jmsConnection.start(); Message message = this.messageConsumer.receive(); if (message instanceof BytesMessage) { BytesMessage bm = (BytesMessage) message; byte[] buf = new byte[1024]; int len = 0; ByteArrayOutputStream content = null; content = new ByteArrayOutputStream(); while ((len = bm.readBytes(buf)) > 0) { content.write(buf, 0, len); } String strMessage = new String(content.toByteArray(), SYSTEM_CHARCTER); System.out.println(strMessage); } this.jmsSession.commit(); this.close(); } catch (JMSException e) { e.getLinkedException().printStackTrace(); e.printStackTrace(); } } public static void main(String[] args) throws JMSException, UnsupportedEncodingException { // 创建连接 ConnectionTest test = new ConnectionTest(); // 发送消息 test.initial(); //test.send("hello queue!"); test.get(); } public void close() { if (this.jmsProducer != null) { try { this.jmsProducer.close(); this.jmsProducer = null; } catch (JMSException e) { e.printStackTrace(); } } } }