/**
* A WebSphere MQ application which uses MQQueueConnectionFactory to send msg to a Queue.
* This MQQueueConnectionFactory is looked up in JNDI Context.
* Before you look up, you should create a JNDI Context using 'WebSphere MQ JMS administration tool'.
* This method to create is provided at other file.
*/
package com.ibm.mq.test
import java.util.Hashtable
import javax.jms.DeliveryMode
import javax.jms.JMSException
import javax.jms.Queue
import javax.jms.QueueConnection
import javax.jms.QueueSender
import javax.jms.QueueSession
import javax.jms.Session
import javax.jms.TextMessage
import javax.naming.Context
import javax.naming.InitialContext
import javax.naming.NamingException
import com.ibm.mq.jms.MQQueueConnectionFactory
public class MQSendByCF_JNDI {
public static void main(String[] args) {
MQSendByCF_JNDI sender = new MQSendByCF_JNDI()
try {
sender.initMQObjects()
sender.sendMsg()
} catch(Exception e) {
e.printStackTrace()
} finally {
sender.closeMQObjects()
}
}
MQQueueConnectionFactory mqcf
QueueConnection conn
QueueSession session
TextMessage textMsg
Queue queue
QueueSender sender
public void initMQObjects() throws Exception {
String strMsg = "Where are you?"
Hashtable env = new Hashtable()
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory")
env.put(Context.PROVIDER_URL, "file:/D:/JNDI")
try {
Context ctx = new InitialContext(env)
mqcf = (MQQueueConnectionFactory) ctx.lookup("QCF_TEST")
/*This Queue [Q_TEST] is not a real queue on QebSphere MQ,
but it is binded to a real queue on WebSphere MQ.*/
queue = (Queue) ctx.lookup("Q_TEST")
} catch (NamingException e) {
System.out.println("Find MQ Objects from Context Failed.")
throw e
}
conn = mqcf.createQueueConnection()
session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE)
textMsg = session.createTextMessage(strMsg)
sender = session.createSender(queue)
}
/**
* Name: sendMsg
* Date: 2010-10-1
* paramter:
* Return_type: void
* User: Leng Fuping
* Description: Send Msg
*/
public void sendMsg() throws Exception {
try {
conn.start()
} catch (JMSException e) {
System.out.println("Send Msg Failed.")
throw e
}
sender.send(textMsg,DeliveryMode.NON_PERSISTENT,0,0)
System.out.println("Send Msg succeed.")
}
/**
* Name: closeMQObjects
* Date: 2010-10-1
* paramter:
* Return_type: void
* User: Leng Fuping
* Description: Close MQ Objects
*/
public void closeMQObjects() {
try {
if(conn != null) {
conn.stop()
}
if(sender != null) {
sender.close()
}
if(session != null) {
session.close()
}
if(conn != null) {
conn.close()
}
if(mqcf != null) {
mqcf.clear()
}
System.out.println("Close MQ objects succeed.")
} catch (JMSException e) {
System.out.println("Close MQ objects failed: " + e.getMessage())
}
}
}
想系统学习下RocketMQ,所谓万事开头难,环境跑起来再说。
虚拟机:VirtualBox
最好不要用 网络地址转换(NAT)端口转发 模式,RocketMQ在程序访问的时候只需要填写一个NameSrv地址,但是会从NameSrv拿Broker的地址(Broker端口10911,假如本机地址 10.10.1.22,虚拟机地址10.10.1.23,你的端口映射:10.10.1.22:10911 ->10.10.1.23:10911,很不幸,RocketMQ会拿到10.10.1.23:10911去尝试连接Broker,这必然是连接不上的,而且MQ报错信息也很简单粗暴,后面也会经常遇到: No route info of this topic XXX )
*** 作系统:Ubuntu 18.04
JDK:1.8
之前采用的JDK12,坑比较多,JDK12对JVM部分做了调整,RocketMQ提供的配置是基于JDK1.8的,改了半天也没有弄好
RocketMQ:4.4.0
搭建很简单,装好JDK,配置好环境变量,然后启动NameSrv及Broker
Rocket目录: ~/soft/rocketmq-all-4.4.0-bin-release ,之后的命令都是在这个目录下执行的
虚拟机IP:10.1.11.115
nohup sh bin/mqnamesrv -n 10.1.11.155:9876 &
启动成功后日志显示:The Name Server boot success. serializeType=JSON
nohup sh bin/mqbroker -n 10.1.11.155:9876 -c conf/broker.confautoCreateTopicEnable=true &
启动成功后日志显示:
The broker[broker-a, 10.1.11.155:10911] boot success. serializeType=JSON and name server is 10.1.11.155:9876
注意斜体部分的配置文件: broker.conf 需要新增三行配置:
第一行指定Name Server地址,第二行及第三行执行Broker的地址,为啥有俩,据说有一个是给VIP通道用的,注意这里用的IP都是具体的IP,建议不要使用 localhost 或者 127.0.0.1,不然有可能出现 No route info of this topic XXX
autoCreateTopicEnable 这个属性,允许自动创建topic,我配置了这个,没有毛线用处,没有深究,都是手工创建的Topic,创建及查看Topic的cmd命令如下:
创建:sh bin/mqadmin updateTopic -n namesrv地址 -b broker地址 -t topic名称
示例:sh bin/mqadmin updateTopic -n 10.1.11.115:9876 -b 10.1.11.115:10911 -t asdTopic
查看所有信息,含Topic :sh mqadmin topicList -n
NameServer 及 Broker启动完毕后,可以用自带的Tools模拟Producer及Consumer,命令如下:
首先要执行 export NAMESRV_ADDR=10.1.11.155:9876, 告诉 Producer 和 Consumer Name Server 的地址
启动Producer,该Producer会产生一批数据推送到MQ
命令:sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
最后启动Consumer接收Producer发送的消息
命令:sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
关闭 broker :sh bin/mqshutdown broker
关闭 name server:sh bin/mqshutdown namesrv
这一篇主要是MQ的搭建及基础验证,参考 http://rocketmq.apache.org/docs/quick-start/ ,下一篇主要是通过JAVA模拟Producer及Consumer。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)