Skip to content

Commit 2744887

Browse files
authored
Merge pull request #24 from ibm-messaging/applname-enhancements
Applname enhancements
2 parents 07a43fe + 37f150d commit 2744887

File tree

6 files changed

+92
-9
lines changed

6 files changed

+92
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ your own error handling or logging.
122122
* Send and receive under a local transaction - [local_transaction_test.go](local_transaction_test.go)
123123
* Sending a message that expires after a period of time - [timetolive_test.go](timetolive_test.go)
124124
* Handle error codes returned by the queue manager - [sample_errorhandling_test.go](sample_errorhandling_test.go)
125+
* Set the application name (ApplName) on connections - [applname_test.go](applname_test.go)
126+
125127

126128
As normal with Go, you can run any individual testcase by executing a command such as;
127129
```bash

applname_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
"testing"
14+
15+
"github.com/ibm-messaging/mq-golang-jms20/mqjms"
16+
"github.com/stretchr/testify/assert"
17+
)
18+
19+
/*
20+
* Test send and receive of a text message with no content.
21+
*/
22+
func TestApplName(t *testing.T) {
23+
24+
// Pick an application name for testing
25+
applName := "MyApplicationName"
26+
27+
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
28+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
29+
assert.Nil(t, cfErr)
30+
31+
// Set the appl name
32+
cf.ApplName = applName
33+
34+
// Creates a connection to the queue manager, using defer to close it automatically
35+
// at the end of the function (if it was created successfully)
36+
context, ctxErr := cf.CreateContext()
37+
assert.Nil(t, ctxErr)
38+
if context != nil {
39+
defer context.Close()
40+
}
41+
42+
// Create a TextMessage, and check it has nil content.
43+
msg := context.CreateTextMessage()
44+
assert.Nil(t, msg.GetText())
45+
46+
// Now send the message and get it back again, to check that it roundtripped.
47+
queue := context.CreateQueue("DEV.QUEUE.1")
48+
errSend := context.CreateProducer().SetTimeToLive(60000).Send(queue, msg)
49+
assert.Nil(t, errSend)
50+
51+
consumer, errCons := context.CreateConsumer(queue)
52+
if consumer != nil {
53+
defer consumer.Close()
54+
}
55+
assert.Nil(t, errCons)
56+
57+
rcvMsg, errRvc := consumer.ReceiveNoWait()
58+
assert.Nil(t, errRvc)
59+
assert.NotNil(t, rcvMsg)
60+
61+
messageImpl, ok := rcvMsg.(*mqjms.TextMessageImpl)
62+
assert.True(t, ok)
63+
64+
// Check that the application name was successfully stored in the message
65+
// that we sent.
66+
assert.Equal(t, applName, messageImpl.GetApplName())
67+
68+
}

mqjms/ConnectionFactoryImpl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type ConnectionFactoryImpl struct {
4242
CertificateLabel string
4343

4444
// Allthough only available per MQ 9.1.2 it looks like a good idea to have this present in MQ-JMS
45-
ApplName string
45+
ApplName string
4646
}
4747

4848
// CreateContext implements the JMS method to create a connection to an IBM MQ

mqjms/FactoryFactory.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
//
2424
// This method reads the following files;
2525
// - $HOME/Downloads/connection_info.json for host/port/channel information
26-
// - $HOME/Downloads/apiKey.json for username/password information
26+
// - $HOME/Downloads/applicationApiKey.json for username/password information
2727
//
2828
// If your queue manager is hosted on the IBM MQ on Cloud service then you can
2929
// download these two files directly from the IBM Cloud service console.
@@ -42,7 +42,7 @@ func CreateConnectionFactoryFromDefaultJSONFiles() (cf ConnectionFactoryImpl, er
4242
// the file as the two parameters. If empty string is provided then the default
4343
// location and name is assumed as follows;
4444
// - $HOME/Downloads/connection_info.json for host/port/channel information
45-
// - $HOME/Downloads/apiKey.json for username/password information
45+
// - $HOME/Downloads/applicationApiKey.json for username/password information
4646
//
4747
// If your queue manager is hosted on the IBM MQ on Cloud service then you can
4848
// download these two files directly from the IBM Cloud service console.
@@ -110,10 +110,8 @@ func CreateConnectionFactoryFromJSON(connectionInfoLocn string, apiKeyLocn strin
110110
return ConnectionFactoryImpl{}, errChannel
111111
}
112112

113-
appName, errAppName := parseStringValueFromJSON("applicationName", connInfoMap, connectionInfoLocn)
114-
if errAppName != nil {
115-
return ConnectionFactoryImpl{}, errAppName
116-
}
113+
// app name is an optional field so we silently ignore if it is not present
114+
appName, _ = parseStringValueFromJSON("applicationName", connInfoMap, connectionInfoLocn)
117115

118116
// Now unmarshall and parse out the values from the api key file (that
119117
// contains the username/password credentials).
@@ -145,7 +143,7 @@ func CreateConnectionFactoryFromJSON(connectionInfoLocn string, apiKeyLocn strin
145143
ChannelName: appChannel,
146144
UserName: username,
147145
Password: password,
148-
ApplName: appName,
146+
ApplName: appName,
149147
}
150148

151149
// Give the populated ConnectionFactory back to the caller.

mqjms/MessageImpl.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,19 @@ func (msg *MessageImpl) GetJMSTimestamp() int64 {
258258

259259
return timestamp
260260
}
261+
262+
// GetApplName retrieves the PutApplName field from the MQMD.
263+
// This method is not exposed on the JMS style interface and is mainly for testing purposes.
264+
func (msg MessageImpl) GetApplName() string {
265+
applName := ""
266+
267+
// Note that if there is no MQMD then there is no correlID stored.
268+
if msg.mqmd != nil {
269+
270+
// Get hold of the bytes representation of the correlation ID.
271+
applName = msg.mqmd.PutApplName
272+
273+
}
274+
275+
return applName
276+
}

next-features.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ Not currently implemented:
1414

1515
Client capabilities for participating in Uniform Clusters;
1616
- CCDT to allow listing queue managers
17-
- Set APPLTag to name the logical application
1817
- Auto reconnect (any) + MQCNO_RECONNECT_QMGR
1918

2019
Known issues:

0 commit comments

Comments
 (0)