Java Worker Example

Introduction

This is a simple example how to use Java workers. Essentially, these samples are same as the ones for Python and other languages.


In [1]:
IP = '192.168.99.100'  # You need to change this address to your container host
BASE = 'http://' + IP + '/v1/'  

import requests
import json

def jprint(data):
    print(json.dumps(data, indent=4))

HEADERS = {'Content-Type': 'application/json'}

# List of all available services
res = requests.get(BASE + 'services')
print("Here is the list of services supported by this submit agent:\n")
jprint(res.json())


Here is the list of services supported by this submit agent:

[
    {
        "description": "NBS worker sample",
        "serviceName": "nbs",
        "parameters": [
            {
                "type": "string",
                "name": "inputFile",
                "description": "Input file location",
                "required": true
            }
        ],
        "portNumber": "5561"
    },
    {
        "description": "Sample Java worker to display greeting message",
        "serviceName": "hello-java",
        "parameters": [
            {
                "type": "string",
                "name": "message",
                "description": "Greeting message",
                "required": true
            }
        ],
        "portNumber": "5560"
    }
]

In [ ]:
# And this is the sample Java Worker
res = requests.get(BASE + 'services/hello-java')
jprint(res.json())

In [52]:
# Submit a job
greeting = {
    'message': 'Hello, this is a message for Java from Python client.'
}

for i in range(10):
    res = requests.post(BASE + 'services/hello-java', data=json.dumps(greeting), headers=HEADERS)

job_id1 = res.json()['job_id']
jprint(res.json())


{
    "status": "queued",
    "job_id": "780dac9e-8cec-482d-8be1-67087c0c6e3b"
}

In [53]:
nbs_input = {
    'inputFile': 'http://www.cytoscape.org/'
}

for i in range(10):
    res = requests.post(BASE + 'services/nbs', data=json.dumps(nbs_input), headers=HEADERS)

job_id1 = res.json()['job_id']
jprint(res.json())


{
    "status": "queued",
    "job_id": "48a7970e-57f9-4101-81e8-bd76e9d09c8d"
}

In [56]:
# Check the job status
res = requests.get(BASE + 'queue')
# job_id1 = res.json()[0]['job_id']
jprint(res.json())


[
    {
        "status": "finished",
        "job_id": "145f3f33-8acc-4b1e-9e31-e619ffde3266"
    },
    {
        "status": "finished",
        "job_id": "9c114d4c-7118-4054-a6b0-644f4b6a96f1"
    },
    {
        "status": "finished",
        "job_id": "d74f2734-6ede-4a2a-842d-2f186fde9c6d"
    },
    {
        "status": "finished",
        "job_id": "673ffc02-14ec-4378-b376-fe1181b21afa"
    },
    {
        "status": "finished",
        "job_id": "5644a8a2-a2e4-460a-a5ad-f6ba63048530"
    },
    {
        "status": "finished",
        "job_id": "27262a47-31f6-42f2-9b20-1894c7c4f82e"
    },
    {
        "status": "finished",
        "job_id": "78809ad1-39f3-400b-b4bf-6eb755e68884"
    },
    {
        "status": "finished",
        "job_id": "780dac9e-8cec-482d-8be1-67087c0c6e3b"
    },
    {
        "status": "finished",
        "job_id": "fb025aab-37c9-41f5-9dbc-6485c22bb3c8"
    },
    {
        "status": "finished",
        "job_id": "9f64f9d5-d9b2-45ce-a539-871be5da0bee"
    },
    {
        "status": "finished",
        "job_id": "48a7970e-57f9-4101-81e8-bd76e9d09c8d"
    },
    {
        "status": "finished",
        "job_id": "02928f9c-a40a-4770-ae6f-252ca58b233f"
    },
    {
        "status": "finished",
        "job_id": "6eef479d-6ecc-4abf-a684-c927194e4438"
    },
    {
        "status": "finished",
        "job_id": "7c580edb-00d2-45a2-b8ab-0ab7638adde7"
    },
    {
        "status": "finished",
        "job_id": "76aca870-f138-4a13-9889-14f38e1d6622"
    },
    {
        "status": "finished",
        "job_id": "e54f74e5-9797-44c0-83ee-e9d01e9c94ec"
    },
    {
        "status": "finished",
        "job_id": "4d557701-5072-4ef6-8c46-4eba951c45e2"
    },
    {
        "status": "finished",
        "job_id": "f59f48c6-d868-4041-9a33-faf087e9a07b"
    },
    {
        "status": "finished",
        "job_id": "2e5b2d29-7668-4a57-98fa-a2b6811323c8"
    },
    {
        "status": "finished",
        "job_id": "72d5b338-0824-4f04-93e4-72b7026896b5"
    }
]

In [49]:
result_url = BASE + 'queue/' + job_id1 + '/result'
print(result_url)
res = requests.get(result_url)
jprint(res.json())


http://192.168.99.100/v1/queue/23b26466-a3d1-484a-9014-51594e584b64/result
{
    "result": "Dummy result"
}

In [50]:
res = requests.delete(BASE + 'queue')