大家。有没有人曾使用https://github.com/ibm-messaging/mq-golang提供的IBM经验来分享?
这是我的代码:
package main
import (
"bufio"
"fmt"
"../ibmmq"
"os"
"strings"
// "time"
)
func main() {
var qMgrName string
var qMgrObject ibmmq.MQObject
var managedQObject ibmmq.MQObject
var subObject ibmmq.MQObject
var err error
var qMgr ibmmq.MQQueueManager
// var rc int
var openOptions int32
var qObject ibmmq.MQObject
if len(os.Args) != 5 {
fmt.Println("clientconn <qmgrname> <channelname> <conname> <queuename>")
fmt.Println("")
fmt.Println("For example")
fmt.Println(" clientconn QMGR1 \"SYSTEM.DEF.SVRCONN\" \"myhost.example.com(1414)\" \"LOCAL.QUEUE\"")
fmt.Println("All parameters are required.")
os.Exit(1)
}
connected := false
qMgrName = os.Args[1]
cno := ibmmq.NewMQCNO()
cd := ibmmq.NewMQCD()
cd.ChannelName = os.Args[2]
cd.ConnectionName = os.Args[3]
cno.ClientConn = cd
cno.Options = ibmmq.MQCNO_CLIENT_BINDING
userId := os.Getenv("MQSAMP_USER_ID")
if userId != "" {
scanner := bufio.NewScanner(os.Stdin)
csp := ibmmq.NewMQCSP()
csp.AuthenticationType = ibmmq.MQCSP_AUTH_USER_ID_AND_PWD
csp.UserId = userId
fmt.Println("Enter password : ")
scanner.Scan()
csp.Password = scanner.Text()
cno.SecurityParms = csp
}
qMgr, err = ibmmq.Connx(qMgrName, cno)
// qMgr, err = ibmmq.Conn(qMgrName)
if err == nil {
fmt.Printf("Connection to %s succeeded.\n", qMgrName)
// d, _ := time.ParseDuration("5s")
// time.Sleep(d)
// qMgr.Disc()
} else {
fmt.Printf("Connection to %s failed.\n", qMgrName)
fmt.Println(err)
}
if err == nil {
mqod := ibmmq.NewMQOD()
openOptions = ibmmq.MQOO_OUTPUT + ibmmq.MQOO_FAIL_IF_QUIESCING
openOptions |= ibmmq.MQOO_INPUT_AS_Q_DEF
mqod.ObjectType = ibmmq.MQOT_Q
mqod.ObjectName = os.Args[4]
qObject, err = qMgr.Open(mqod, openOptions)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Opened queue", qObject.Name)
}
}
if err == nil {
putmqmd := ibmmq.NewMQMD()
pmo := ibmmq.NewMQPMO()
pmo.Options = ibmmq.MQPMO_SYNCPOINT | ibmmq.MQPMO_NEW_MSG_ID | ibmmq.MQPMO_NEW_CORREL_ID
putmqmd.Format = "MQSTR"
msgData := "Hello from Go"
buffer := []byte(msgData)
// mqod := ibmmq.NewMQOD()
// openOptions = ibmmq.MQOO_INQUIRE + ibmmq.MQOO_FAIL_IF_QUIESCING
// mqod.ObjectType = ibmmq.MQOT_CHANNEL
// mqod.ObjectName = qObject.Name
err = qObject.Put(putmqmd, pmo, buffer)
// err = qMgr.Put1(mqod, putmqmd, pmo, buffer)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Put message to", qObject.Name)
}
}
if err == nil {
err = qMgr.Cmit()
if err != nil {
fmt.Println(err)
}
}
if err == nil {
msgAvail := true
for msgAvail == true {
var datalen int
getmqmd := ibmmq.NewMQMD()
gmo := ibmmq.NewMQGMO()
gmo.Options = ibmmq.MQGMO_NO_SYNCPOINT | ibmmq.MQGMO_FAIL_IF_QUIESCING
gmo.Options |= ibmmq.MQGMO_WAIT
gmo.WaitInterval = 3000
buffer := make([]byte, 32768)
datalen, err = qObject.Get(getmqmd, gmo, buffer)
if err != nil {
msgAvail = false
fmt.Println(err)
mqret := err.(*ibmmq.MQReturn)
if mqret.MQRC == ibmmq.MQRC_NO_MSG_AVAILABLE {
err = nil
}
} else {
fmt.Printf("Got message of length %d: ", datalen)
fmt.Println(strings.TrimSpace(string(buffer[:datalen])))
}
}
}
if err == nil {
err = qObject.Close()
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Closed queue")
}
}
if err == nil {
mqsd := ibmmq.NewMQSD()
mqsd.Options = ibmmq.MQSO_CREATE
mqsd.Options |= ibmmq.MQSO_NON_DURABLE
mqsd.Options |= ibmmq.MQSO_FAIL_IF_QUIESCING
mqsd.Options |= ibmmq.MQSO_MANAGED
mqsd.ObjectString = "$SYS/MQ/INFO/QMGR/" + qMgrName + "/ActivityTrace/ApplName/mqitest"
subObject, err = qMgr.Sub(mqsd, &managedQObject)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Subscribed to topic ", mqsd.ObjectString)
}
}
if err == nil {
msgAvail := true
for msgAvail == true {
var datalen int
getmqmd := ibmmq.NewMQMD()
gmo := ibmmq.NewMQGMO()
gmo.Options = ibmmq.MQGMO_NO_SYNCPOINT | ibmmq.MQGMO_FAIL_IF_QUIESCING
gmo.Options |= ibmmq.MQGMO_WAIT
gmo.WaitInterval = 3000
buffer := make([]byte, 32768)
datalen, err = managedQObject.Get(getmqmd, gmo, buffer)
if err != nil {
msgAvail = false
fmt.Println(err)
mqret := err.(*ibmmq.MQReturn)
if mqret.MQRC == ibmmq.MQRC_NO_MSG_AVAILABLE {
// not a real error so reset err, but
// end retrieval loop
err = nil
}
} else {
fmt.Printf("Got message of length %d. Format = %s\n", datalen, getmqmd.Format)
}
}
}
if err == nil {
subObject.Close()
}
if err == nil {
mqod := ibmmq.NewMQOD()
openOptions = ibmmq.MQOO_INQUIRE + ibmmq.MQOO_FAIL_IF_QUIESCING
mqod.ObjectType = ibmmq.MQOT_Q_MGR
mqod.ObjectName = qMgrName
qMgrObject, err = qMgr.Open(mqod, openOptions)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("Opened QMgr for MQINQ\n")
}
}
if err == nil {
selectors := []int32{ibmmq.MQCA_Q_MGR_NAME,
ibmmq.MQCA_DEAD_LETTER_Q_NAME,
ibmmq.MQIA_MSG_MARK_BROWSE_INTERVAL}
intAttrs, charAttrs, err := qMgrObject.Inq(selectors, 2, 160)
if err != nil {
fmt.Println(err)
} else {
returnedName := string(charAttrs[0:48])
fmt.Printf("MQINQ returned +%v %s \n",
intAttrs, string(charAttrs))
fmt.Printf(" '%s'\n", returnedName)
}
}
if connected {
err = qMgr.Disc()
fmt.Println("Disconnected from queue manager ", qMgrName)
}
if err == nil {
os.Exit(0)
} else {
mqret := err.(*ibmmq.MQReturn)
os.Exit((int)(mqret.MQCC))
}
}
但总是显示致命错误:
[root@localhost clientconn]# ./clientconn QM_T01 "JMS" "127.0.0.1(1414)" "LQ"
Connection to QM_T01 succeeded.
Opened queue LQ
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x7f214ba2b414]
runtime stack: runtime.throw(0x4ebdce, 0x2a)
/home/app/go/src/runtime/panic.go:605 +0x95 runtime.sigpanic()
/home/app/go/src/runtime/signal_unix.go:351 +0x2b8
goroutine 1 [syscall, locked to thread]: runtime.cgocall(0x49ed30,
0xc42004f9c8, 0x100) /home/app/go/src/runtime/cgocall.go:132 +0xe4
fp=0xc42004f980 sp=0xc42004f940 pc=0x4072c4
_/svr/ibmmq/ibmmq._Cfunc_MQPUT(0x1000006, 0xc42007c000, 0xc4200720c0, 0xd, 0xc420016110, 0xc420016120, 0xc4200160fc)
_/svr/ibmmq/ibmmq/_obj/_cgo_gotypes.go:887 +0x45 fp=0xc42004f9c8
sp=0xc42004f980 pc=0x4938e5
_/svr/ibmmq/ibmmq.MQObject.Put.func1(0x1000006, 0xc42007c000, 0xc4200720c0, 0xc40000000d, 0xc420016110, 0xc420016120, 0xc4200160fc)
/svr/ibmmq/ibmmq/mqi.go:407 +0x10e fp=0xc42004fa10 sp=0xc42004f9c8
pc=0x49a69e
_/svr/ibmmq/ibmmq.MQObject.Put(0x0, 0xc42000c0a0, 0xc42001a0f0, 0x30, 0xc420078000, 0xc42007a000, 0xc420016110, 0xd, 0x10, 0x0, ...)
/svr/ibmmq/ibmmq/mqi.go:407 +0x120 fp=0xc42004fa80 sp=0xc42004fa10
pc=0x495030 main.main() /svr/ibmmq/clientconn/clientconn.go:102
+0x158e fp=0xc42004ff80 sp=0xc42004fa80 pc=0x49e29e runtime.main() /home/app/go/src/runtime/proc.go:195 +0x226 fp=0xc42004ffe0
sp=0xc42004ff80 pc=0x42e9c6 runtime.goexit()
/home/app/go/src/runtime/asm_amd64.s:2337 +0x1 fp=0xc42004ffe8
sp=0xc42004ffe0 pc=0x457491
答案 0 :(得分:0)
mq-golang的自述文件声明您必须至少具有IBM MQ v9。
构建此程序包需要最低级别的MQ V9。队列管理器发布的监视数据在该版本之前不可用;该接口还假定MQ级别的MQI结构可用。
答案 1 :(得分:0)
我尝试将最新的MQ 9.1.1 CD版本与go1.11.5结合使用,我不得不对您的代码进行一些修改,以便在Close()调用中添加int32使其能够进行编译,但是现在可以编译了并可以在我的Ubuntu 16.04 VM上正常运行。
如果使用更高版本的MQ重新构建MQ Go客户端,则应在go install上使用-a标志来强制其重新构建所有内容,而不是缓存已构建的位,您仍然可以使用针对旧版本编译的代码MQ库。