Skip to content

Service

Purpose

List and perform request/response exchanges with deployed Platform 6 services.

Note

Use the Common Message Bus API Guide to determine what requests are valid for given services/endpoints.

Methods

Binding name: p6.service


post

Sends the provided common message to the named service destination endpoint. This method is fire and forget. I.e. It does not wait for a response.

Syntaxes

void p6.service.post(String destination, CommonMessage cm)
void p6.service.post(String destination, Map cmMap)
Parameter: cmMap
Example
final def cm = p6.service.cm.build.response(true, 'Hello world')
p6.service.post('platform6.echo', cm)
p6.service.post('platform6.echo', [headers: [
    'platform6.response.status': 'true',
    'platform6.response.value': 'Hello world'
]])

request

With wait

Sends the provided common message to the named service destination endpoint and waits indefinitely for a response which is returned as a Map.

Syntaxes

Map p6.service.request(String destination, CommonMessage cm)
Map p6.service.request(String destination, Map cmMap)
Parameter: cmMap
Example
final def message = p6.service.cm.build.request.service 'display'
p6.service.cm.header.add body : 'Hello world' to message        
final def cmResponse2 = p6.service.request('platform6.echo', message)
p6.log.log cmResponse2
final def cmResponse = p6.service.request('platform6.echo', [headers: [
    'platform6.request.action': 'display',
    'body': 'Hello world'
]])
p6.log.log cmResponse

With timeout

Sends the provided common message to the named service destination endpoint and waiting for up to timeOut time units for a response. Any response is returned as a Map.

Syntaxes

Map p6.service.request(String destination, CommonMesage cm, long timeOut, TimeUnit tu)
Map p6.service.request(String destination, Map cmMap, long timeOut, TimeUnit tu)
Parameter: cmMap
Example
import java.util.concurrent.TimeUnit
final def message = p6.service.cm.build.request.service 'list.ids'    
final def cmResponse2 = p6.service.request('platform6.scripts', message, 5, TimeUnit.SECONDS)
p6.log.info cmResponse2
import java.util.concurrent.TimeUnit

final def cm = [headers: [
     'platform6.request.action': 'list.ids'
]]
final def cmResponse = p6.service.request('platform6.scripts', cm, 5, TimeUnit.SECONDS)
p6.log.info cmResponse        

With Callback

Sends the provided common message to the named service destination endpoint and waits indefinitely for a response which is returned as a Map via the supplied asynchronous callback.

Syntax

void p6.service.request(String destination, Map cmMap, Closure cb)
Parameter: cmMap

Info

The Callback will receive one of two parameters in the following order (CommonMessageMap, Exception).

Example
final def message = p6.service.cm.build.request.service 'status'    
p6.service.request('platform6.scripts', message, {
    cmr, e ->
        println cmr
        if( null != e) throw e
})

sleep 2000
final def cm = [headers: [
     'platform6.request.action': 'status'
 ]]
p6.service.request('platform6.scripts', cm, {
    cmr, e ->
        println cmr
        if( null != e) throw e
})

sleep 2000

list

Returns a List of all deployed Platform 6 services attached to the common message bus represented as destination names.

Syntax

List p6.service.list()
Example
p6.service.list().each() {
  p6.log.debug it + ' : '' + p6.service.getServiceStatus(it)
}

getServiceStatus

Get the service’s status.

Syntax

String p6.service.getServiceStatus( String serviceId )

Info

The status can be: SERVICE_STATE_STARTED, SERVICE_STATE_STARTED_RESTART (if the configuration has changed) and SERVICE_STATE_STOPPED.

Example
p6.log.debug p6.service.getServiceStatus('routes')

startService

Start the serviceId service.

Syntax

void p6.service.startService( String serviceId )
Example
p6.service.startService 'platform6.routes'

stopService

Stop the serviceId service.

Syntax

void p6.service.stopService( String serviceId )
Example
p6.service.stopService 'platform6.routes'

restartService

Restart the serviceId service.

Syntax

void p6.service.restartService( String serviceId )
Example
p6.service.restartService 'platform6.routes'

cm.build.response

Since 6.10.12

Create a common message response.

Note

To respond directly to a given request use the pipeline, cm responses must be sent to specified endpoints via another post/request.

Syntax

CommonMessage p6.service.cm.build.response ( boolean success, String message )
Example
p6.service.cm.build.response(true, 'Hello world')
p6.service.cm.build.response(false, 'An error occurred')

cm.build.exception

Since 6.10.12

Create a common message exception response.

Syntax

CommonMessage p6.service.cm.build.exception( Exception e )
CommonMessage p6.service.cm.build.exception( String reason, String message )
Example
final def cm = p6.service.cm.build.exception e
final def cm = p6.service.cm.build.exception('reason', 'message')

cm.build.request

Since 6.10.12

Create a common message request.

Syntax

Common Message p6.service.cm.build.request.service String action
Common Message p6.service.cm.build.request.connector String action
Example
final def cm = p6.service.cm.build.request.service 'list'
p6.service.cm.header.add fee: 'foo', foo: ['bar', 'bee'] to cm

It populate the common message with a platform6.request.action header set to list.

final def cm = p6.service.cm.build.request.connector 'list'
p6.service.cm.header.add fee: 'foo', foo: ['bar', 'bee'] to cm

It populate the common message with a platform6.connector.action header set to list.


cm.header.add

Since 6.10.12

Append headers to an existing common message.

Syntax

void p6.service.cm.header.add Map<String, Object> headers to CommonMessage message
void p6.service.cm.header.add (String key, String value) to CommonMessage message
Example
final def cm = p6.service.cm.build.response(true, 'Hello world')
p6.service.cm.header.add('fee', 'foo') to cm
p6.service.cm.header.add fee:'foo' to cm
p6.service.cm.header.add fee:'foo', foo: ['bar', 'bee'], bar: {boo: 'bee'} to cm

cm.attachment.add

Since 6.10.12

Add an attachment to the common message.

optional params:

Params: String - contentType, String - name, Map - headers
Default: contentType = text/plain

Syntax

void p6.service.cm.attachment.add String uri to CommonMessage 

Note

If missing, the default p6file:/ protocol will be added to the uri.

void p6.service.cm.attachment.add File file to CommonMessage
Example
final def cm = p6.service.cm.build.response(true, 'Hello world')
p6.service.cm.attachment.add 'p6file://${P6_DATA}/path/to/file.txt' to cm
p6.service.cm.attachment.add 'p6file://${P6_DATA}/path/to/file.txt' to cm, { contentType: 'text/plain' }
p6.service.cm.attachment.add 'p6file://${P6_DATA}/path/to/file.txt' to cm, { contentType: 'text/plain'; name: 'invoice.txt' }
p6.service.cm.attachment.add 'p6file://${P6_DATA}/path/to/file.txt' to cm, { headers: [fee: 'foo'] }
final def file = new File('path/to/file.txt')
final def cm = p6.service.cm.build.response(true, 'Hello world')
p6.service.cm.attachment.add file to cm
p6.service.cm.attachment.add file to cm, { contentType: 'text/plain' }
p6.service.cm.attachment.add file to cm, { contentType: 'text/plain'; name: 'invoice.txt' }
p6.service.cm.attachment.add file to cm, { headers: [fee: 'foo'] }

cm.attachment.save

Since 6.10.12

Save an attachment from a common message to a file.

Syntax

void p6.service.cm.attachment.save String attachementName from CommonMessage cm to String uri

Note

If missing, the default p6file:/ protocol will be added to the uri.

void p6.service.cm.attachment.save String attachementName from CommonMessage cm to File target
Example
final def cm = p6.service.cm.build.response(true, 'Hello world')
p6.service.cm.attachment.add 'p6file://${P6_DATA}/path/to/source.txt' to cm
p6.service.cm.attachment.save 'source.txt' from cm to 'p6file://${P6_DATA}/path/to/target.txt'
final def target = new File('path/to/target.txt')
final def cm = p6.service.cm.build.response(true, 'Hello world')
p6.service.cm.attachment.add 'p6file://${P6_DATA}/path/to/source.txt' to cm
p6.service.cm.attachment.save 'source.txt' from cm to target

cm.attachment.remove

Since 6.10.12

Removes an attachments of a common message based on his name.

Syntax

void p6.service.cm.attachment.remove String attachementName from CommonMessage cm
Example
final def cm = p6.service.cm.build.response(true, 'Hello world')
p6.service.cm.attachment.add 'p6file://${P6_DATA}/path/to/source.txt' to cm
p6.service.cm.attachment.remove 'source.txt' from cm

cm.attachment.clear

Since 6.10.12

Removes all the attachments of a common message.

Syntax

void p6.service.cmRemoveAttachments( CommonMessage cm )
Example
final def cm = p6.service.cm.build.response(true, 'Hello world')
p6.service.cm.attachment.add 'p6file://${P6_DATA}/path/to/source1.txt' to cm
p6.service.cm.attachment.add 'p6file://${P6_DATA}/path/to/source2.txt' to cm
p6.service.cm.attachment.add 'p6file://${P6_DATA}/path/to/source3.txt' to cm
p6.service.cm.attachment.clear cm

Parameters

Common Message Maps

The Common Message is the single unit of exchange between Platform 6 service. The common message is represent by a Groovy Map when using the service DSL:

Example

final def cm = [
    headers: [
        hello: 'world',
        'platform6.request.user': 'admin'
    ],
    attachments: [
        [
            headers: [
                name: 'body',
                type: 'text/plain'
            ],
            bytes: 'This is the content of the attachment'.getBytes()
        ]
    ]
]
The map contains a headers Map and an attachments List. The attachments List can contain a headers Map and a bytes content.

Both headers and attachments are optional.