Skip to content

Commit c12f9a5

Browse files
authored
Merge pull request #55 from azarc-io/feature/add-context-creation-options
Added MQ Connection Options to allow configuring of connection programatically
2 parents 044c73a + 44b5720 commit c12f9a5

File tree

6 files changed

+112
-18
lines changed

6 files changed

+112
-18
lines changed

dummy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// +build ignore+
1+
//go:build ignore
2+
// +build ignore
23

34
package main
45

go.sum

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/ibm-messaging/mq-golang v1.0.1-0.20190820103725-19b946c185a8 h1:kUwSXeftVen12FRnShG+Ykhb2Kd6Cd/DbpWwbYal7j0=
4-
github.com/ibm-messaging/mq-golang v1.0.1-0.20190820103725-19b946c185a8/go.mod h1:qjsZDb7m1oKnbPeDma2JVJTKgyCA91I4bcJ1qHY+gcA=
5-
github.com/ibm-messaging/mq-golang v3.0.0+incompatible h1:Yc3c8emAyveT54uNDRMkgvS+EBAHeLNWHkc3hk5x+IY=
6-
github.com/ibm-messaging/mq-golang v3.0.0+incompatible/go.mod h1:qjsZDb7m1oKnbPeDma2JVJTKgyCA91I4bcJ1qHY+gcA=
7-
github.com/ibm-messaging/mq-golang/v5 v5.0.0 h1:9J8bsDoCo60rbSgB7ZAURPG3L5Kpr+F8dYNOwQ7Qnnk=
8-
github.com/ibm-messaging/mq-golang/v5 v5.0.0/go.mod h1:ywCwmYbJOU/E0rl+z4GiNoxVMty68O+LVO39a1VMXrE=
9-
github.com/ibm-messaging/mq-golang/v5 v5.1.2 h1:u0e1Vce2TNqJpH088vF77rDMsnMRWnGaOIlxZo4DMZc=
10-
github.com/ibm-messaging/mq-golang/v5 v5.1.2/go.mod h1:ywCwmYbJOU/E0rl+z4GiNoxVMty68O+LVO39a1VMXrE=
11-
github.com/ibm-messaging/mq-golang/v5 v5.1.3 h1:B1Xfk1jMulmDfPf3jH2Dh9nnyEkaqfI1FvTHt4pFza4=
12-
github.com/ibm-messaging/mq-golang/v5 v5.1.3/go.mod h1:ywCwmYbJOU/E0rl+z4GiNoxVMty68O+LVO39a1VMXrE=
133
github.com/ibm-messaging/mq-golang/v5 v5.2.4 h1:Sn+XpnhlTzBs/xPRAHzSx+YjCdxhmzJu5bv4pWTWu/I=
144
github.com/ibm-messaging/mq-golang/v5 v5.2.4/go.mod h1:ywCwmYbJOU/E0rl+z4GiNoxVMty68O+LVO39a1VMXrE=
155
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
166
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
17-
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
187
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
198
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
209
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=

jms20subset/ConnectionFactory.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ type ConnectionFactory interface {
1818

1919
// CreateContext creates a connection to the messaging provider using the
2020
// configuration parameters that are encapsulated by this ConnectionFactory.
21+
// Optional options can be provided to configure the connection prior to initialisation.
2122
//
2223
// Defaults to sessionMode of JMSContextAUTOACKNOWLEDGE
23-
CreateContext() (JMSContext, JMSException)
24+
CreateContext(opts ...MQOptions) (JMSContext, JMSException)
2425

2526
// CreateContextWithSessionMode creates a connection to the messaging provider using the
2627
// configuration parameters that are encapsulated by this ConnectionFactory,
27-
// and the specified session mode.
28-
CreateContextWithSessionMode(sessionMode int) (JMSContext, JMSException)
28+
// and the specified session mode. Optional options can be provided to configure the
29+
// connection prior to initialisation.
30+
CreateContextWithSessionMode(sessionMode int, opts ...MQOptions) (JMSContext, JMSException)
2931
}

jms20subset/MQOptions.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package jms20subset
2+
3+
import "github.com/ibm-messaging/mq-golang/v5/ibmmq"
4+
5+
type MQOptions func(cno *ibmmq.MQCNO)
6+
7+
func WithMaxMsgLength(maxMsgLength int32) MQOptions {
8+
return func(cno *ibmmq.MQCNO) {
9+
cno.ClientConn.MaxMsgLength = maxMsgLength
10+
}
11+
}

mq_connection_options_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) IBM Corporation 2019
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*/
10+
package main
11+
12+
import (
13+
"github.com/ibm-messaging/mq-golang-jms20/jms20subset"
14+
"github.com/ibm-messaging/mq-golang/v5/ibmmq"
15+
"testing"
16+
17+
"github.com/ibm-messaging/mq-golang-jms20/mqjms"
18+
"github.com/stretchr/testify/assert"
19+
)
20+
21+
func TestMQConnectionOptions(t *testing.T) {
22+
23+
t.Run("MaxMsgLength set on CreateContext", func(t *testing.T) {
24+
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
25+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
26+
assert.Nil(t, cfErr)
27+
28+
// Ensure that the options were applied when setting connection options on Context creation
29+
msg := "options were not applied"
30+
context, ctxErr := cf.CreateContext(
31+
jms20subset.WithMaxMsgLength(2000),
32+
func(cno *ibmmq.MQCNO) {
33+
assert.Equal(t, int32(2000), cno.ClientConn.MaxMsgLength)
34+
msg = "options applied"
35+
},
36+
)
37+
assert.Nil(t, ctxErr)
38+
39+
if context != nil {
40+
defer context.Close()
41+
}
42+
43+
assert.Equal(t, "options applied", msg)
44+
})
45+
46+
t.Run("MaxMsgLength is respected when receive message on CreateContextWithSessionMode", func(t *testing.T) {
47+
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
48+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
49+
assert.Nil(t, cfErr)
50+
51+
sContext, ctxErr := cf.CreateContext()
52+
assert.Nil(t, ctxErr)
53+
if sContext != nil {
54+
defer sContext.Close()
55+
}
56+
57+
// Create a Queue object that points at an IBM MQ queue
58+
sQueue := sContext.CreateQueue("DEV.QUEUE.1")
59+
// Send a message to the queue that contains a large string
60+
errSend := sContext.CreateProducer().SendString(sQueue, "This has more than data than the max message length")
61+
assert.NoError(t, errSend)
62+
63+
// create consumer with low msg length
64+
rContext, ctxErr := cf.CreateContextWithSessionMode(
65+
jms20subset.JMSContextAUTOACKNOWLEDGE,
66+
jms20subset.WithMaxMsgLength(20),
67+
)
68+
assert.Nil(t, ctxErr)
69+
if rContext != nil {
70+
defer rContext.Close()
71+
}
72+
73+
// Create a Queue object that points at an IBM MQ queue
74+
rQueue := rContext.CreateQueue("DEV.QUEUE.1")
75+
// Send a message to the queue that contains a large string
76+
consumer, errSend := rContext.CreateConsumer(rQueue)
77+
assert.NoError(t, errSend)
78+
79+
// expect that receiving the message will cause an JMS Data Length error
80+
_, err := consumer.ReceiveStringBodyNoWait()
81+
assert.Error(t, err)
82+
jmsErr, ok := err.(jms20subset.JMSExceptionImpl)
83+
assert.True(t, ok)
84+
assert.Equal(t, "MQRC_DATA_LENGTH_ERROR", jmsErr.GetReason())
85+
})
86+
}

mqjms/ConnectionFactoryImpl.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ type ConnectionFactoryImpl struct {
5959

6060
// CreateContext implements the JMS method to create a connection to an IBM MQ
6161
// queue manager.
62-
func (cf ConnectionFactoryImpl) CreateContext() (jms20subset.JMSContext, jms20subset.JMSException) {
63-
return cf.CreateContextWithSessionMode(jms20subset.JMSContextAUTOACKNOWLEDGE)
62+
func (cf ConnectionFactoryImpl) CreateContext(mqos ...jms20subset.MQOptions) (jms20subset.JMSContext, jms20subset.JMSException) {
63+
return cf.CreateContextWithSessionMode(jms20subset.JMSContextAUTOACKNOWLEDGE, mqos...)
6464
}
6565

6666
// CreateContextWithSessionMode implements the JMS method to create a connection to an IBM MQ
6767
// queue manager using the specified session mode.
68-
func (cf ConnectionFactoryImpl) CreateContextWithSessionMode(sessionMode int) (jms20subset.JMSContext, jms20subset.JMSException) {
68+
func (cf ConnectionFactoryImpl) CreateContextWithSessionMode(sessionMode int, mqos ...jms20subset.MQOptions) (jms20subset.JMSContext, jms20subset.JMSException) {
6969

7070
// Allocate the internal structures required to create an connection to IBM MQ.
7171
cno := ibmmq.NewMQCNO()
@@ -131,6 +131,11 @@ func (cf ConnectionFactoryImpl) CreateContextWithSessionMode(sessionMode int) (j
131131

132132
}
133133

134+
// Apply options
135+
for _, mqo := range mqos {
136+
mqo(cno)
137+
}
138+
134139
var ctx jms20subset.JMSContext
135140
var retErr jms20subset.JMSException
136141

0 commit comments

Comments
 (0)