|
| 1 | +// Copyright (c) IBM Corporation 2022. |
| 2 | +// |
| 3 | +// This program and the accompanying materials are made available under the |
| 4 | +// terms of the Eclipse Public License 2.0, which is available at |
| 5 | +// http://www.eclipse.org/legal/epl-2.0. |
| 6 | +// |
| 7 | +// SPDX-License-Identifier: EPL-2.0 |
| 8 | + |
| 9 | +// Package mqjms provides the implementation of the JMS style Golang interfaces to communicate with IBM MQ. |
| 10 | +package mqjms |
| 11 | + |
| 12 | +import ( |
| 13 | + "github.com/ibm-messaging/mq-golang-jms20/jms20subset" |
| 14 | + ibmmq "github.com/ibm-messaging/mq-golang/v5/ibmmq" |
| 15 | +) |
| 16 | + |
| 17 | +// BrowserImpl represents the JMS QueueBrowser object that allows applications |
| 18 | +// to peek at messages on a queue without destructively consuming them. |
| 19 | +type BrowserImpl struct { |
| 20 | + browseOption *int32 |
| 21 | + ConsumerImpl // Browser is a specialized form of consumer |
| 22 | +} |
| 23 | + |
| 24 | +// GetEnumeration returns an iterator for browsing the current |
| 25 | +// queue messages in the order they would be received. |
| 26 | +// |
| 27 | +// In this implementation there is exactly one Enumeration per |
| 28 | +// QueueBrowser. If an application wants to browse two independent |
| 29 | +// copies of the messages it must create two QueueBrowsers. |
| 30 | +func (browser *BrowserImpl) GetEnumeration() (jms20subset.MessageIterator, jms20subset.JMSException) { |
| 31 | + |
| 32 | + // A browser is just an alternative view of a Consumer that |
| 33 | + // presents slightly different functions + behaviour. |
| 34 | + return browser, nil |
| 35 | + |
| 36 | +} |
| 37 | + |
| 38 | +// GetNext returns the next Message that is available |
| 39 | +// or else nil if no messages are available. |
| 40 | +func (browser *BrowserImpl) GetNext() (jms20subset.Message, jms20subset.JMSException) { |
| 41 | + |
| 42 | + // Like a ReceiveNoWait, but with Browse turned on. |
| 43 | + gmo := ibmmq.NewMQGMO() |
| 44 | + gmo.Options |= *browser.browseOption |
| 45 | + |
| 46 | + msg, err := browser.receiveInternal(gmo) |
| 47 | + |
| 48 | + if err == nil { |
| 49 | + // After we have browsed the first message successfully we move on to asking |
| 50 | + // for the "next" message from this point onwards. |
| 51 | + brse := int32(ibmmq.MQGMO_BROWSE_NEXT) |
| 52 | + browser.browseOption = &brse |
| 53 | + } |
| 54 | + |
| 55 | + return msg, err |
| 56 | +} |
0 commit comments