Skip to content

Commit

Permalink
Merge pull request #55 from azarc-io/feature/add-context-creation-opt…
Browse files Browse the repository at this point in the history
…ions

Added MQ Connection Options to allow configuring of connection programatically
  • Loading branch information
matrober-uk authored Nov 5, 2022
2 parents 044c73a + 44b5720 commit c12f9a5
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 18 deletions.
3 changes: 2 additions & 1 deletion dummy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// +build ignore+
//go:build ignore
// +build ignore

package main

Expand Down
11 changes: 0 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ibm-messaging/mq-golang v1.0.1-0.20190820103725-19b946c185a8 h1:kUwSXeftVen12FRnShG+Ykhb2Kd6Cd/DbpWwbYal7j0=
github.com/ibm-messaging/mq-golang v1.0.1-0.20190820103725-19b946c185a8/go.mod h1:qjsZDb7m1oKnbPeDma2JVJTKgyCA91I4bcJ1qHY+gcA=
github.com/ibm-messaging/mq-golang v3.0.0+incompatible h1:Yc3c8emAyveT54uNDRMkgvS+EBAHeLNWHkc3hk5x+IY=
github.com/ibm-messaging/mq-golang v3.0.0+incompatible/go.mod h1:qjsZDb7m1oKnbPeDma2JVJTKgyCA91I4bcJ1qHY+gcA=
github.com/ibm-messaging/mq-golang/v5 v5.0.0 h1:9J8bsDoCo60rbSgB7ZAURPG3L5Kpr+F8dYNOwQ7Qnnk=
github.com/ibm-messaging/mq-golang/v5 v5.0.0/go.mod h1:ywCwmYbJOU/E0rl+z4GiNoxVMty68O+LVO39a1VMXrE=
github.com/ibm-messaging/mq-golang/v5 v5.1.2 h1:u0e1Vce2TNqJpH088vF77rDMsnMRWnGaOIlxZo4DMZc=
github.com/ibm-messaging/mq-golang/v5 v5.1.2/go.mod h1:ywCwmYbJOU/E0rl+z4GiNoxVMty68O+LVO39a1VMXrE=
github.com/ibm-messaging/mq-golang/v5 v5.1.3 h1:B1Xfk1jMulmDfPf3jH2Dh9nnyEkaqfI1FvTHt4pFza4=
github.com/ibm-messaging/mq-golang/v5 v5.1.3/go.mod h1:ywCwmYbJOU/E0rl+z4GiNoxVMty68O+LVO39a1VMXrE=
github.com/ibm-messaging/mq-golang/v5 v5.2.4 h1:Sn+XpnhlTzBs/xPRAHzSx+YjCdxhmzJu5bv4pWTWu/I=
github.com/ibm-messaging/mq-golang/v5 v5.2.4/go.mod h1:ywCwmYbJOU/E0rl+z4GiNoxVMty68O+LVO39a1VMXrE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down
8 changes: 5 additions & 3 deletions jms20subset/ConnectionFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ type ConnectionFactory interface {

// CreateContext creates a connection to the messaging provider using the
// configuration parameters that are encapsulated by this ConnectionFactory.
// Optional options can be provided to configure the connection prior to initialisation.
//
// Defaults to sessionMode of JMSContextAUTOACKNOWLEDGE
CreateContext() (JMSContext, JMSException)
CreateContext(opts ...MQOptions) (JMSContext, JMSException)

// CreateContextWithSessionMode creates a connection to the messaging provider using the
// configuration parameters that are encapsulated by this ConnectionFactory,
// and the specified session mode.
CreateContextWithSessionMode(sessionMode int) (JMSContext, JMSException)
// and the specified session mode. Optional options can be provided to configure the
// connection prior to initialisation.
CreateContextWithSessionMode(sessionMode int, opts ...MQOptions) (JMSContext, JMSException)
}
11 changes: 11 additions & 0 deletions jms20subset/MQOptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package jms20subset

import "github.com/ibm-messaging/mq-golang/v5/ibmmq"

type MQOptions func(cno *ibmmq.MQCNO)

func WithMaxMsgLength(maxMsgLength int32) MQOptions {
return func(cno *ibmmq.MQCNO) {
cno.ClientConn.MaxMsgLength = maxMsgLength
}
}
86 changes: 86 additions & 0 deletions mq_connection_options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) IBM Corporation 2019
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package main

import (
"github.com/ibm-messaging/mq-golang-jms20/jms20subset"
"github.com/ibm-messaging/mq-golang/v5/ibmmq"
"testing"

"github.com/ibm-messaging/mq-golang-jms20/mqjms"
"github.com/stretchr/testify/assert"
)

func TestMQConnectionOptions(t *testing.T) {

t.Run("MaxMsgLength set on CreateContext", func(t *testing.T) {
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, cfErr)

// Ensure that the options were applied when setting connection options on Context creation
msg := "options were not applied"
context, ctxErr := cf.CreateContext(
jms20subset.WithMaxMsgLength(2000),
func(cno *ibmmq.MQCNO) {
assert.Equal(t, int32(2000), cno.ClientConn.MaxMsgLength)
msg = "options applied"
},
)
assert.Nil(t, ctxErr)

if context != nil {
defer context.Close()
}

assert.Equal(t, "options applied", msg)
})

t.Run("MaxMsgLength is respected when receive message on CreateContextWithSessionMode", func(t *testing.T) {
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
assert.Nil(t, cfErr)

sContext, ctxErr := cf.CreateContext()
assert.Nil(t, ctxErr)
if sContext != nil {
defer sContext.Close()
}

// Create a Queue object that points at an IBM MQ queue
sQueue := sContext.CreateQueue("DEV.QUEUE.1")
// Send a message to the queue that contains a large string
errSend := sContext.CreateProducer().SendString(sQueue, "This has more than data than the max message length")
assert.NoError(t, errSend)

// create consumer with low msg length
rContext, ctxErr := cf.CreateContextWithSessionMode(
jms20subset.JMSContextAUTOACKNOWLEDGE,
jms20subset.WithMaxMsgLength(20),
)
assert.Nil(t, ctxErr)
if rContext != nil {
defer rContext.Close()
}

// Create a Queue object that points at an IBM MQ queue
rQueue := rContext.CreateQueue("DEV.QUEUE.1")
// Send a message to the queue that contains a large string
consumer, errSend := rContext.CreateConsumer(rQueue)
assert.NoError(t, errSend)

// expect that receiving the message will cause an JMS Data Length error
_, err := consumer.ReceiveStringBodyNoWait()
assert.Error(t, err)
jmsErr, ok := err.(jms20subset.JMSExceptionImpl)
assert.True(t, ok)
assert.Equal(t, "MQRC_DATA_LENGTH_ERROR", jmsErr.GetReason())
})
}
11 changes: 8 additions & 3 deletions mqjms/ConnectionFactoryImpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ type ConnectionFactoryImpl struct {

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

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

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

}

// Apply options
for _, mqo := range mqos {
mqo(cno)
}

var ctx jms20subset.JMSContext
var retErr jms20subset.JMSException

Expand Down

0 comments on commit c12f9a5

Please sign in to comment.