1. “Hello world!” Service¶
In this paragraph we’re going to create a service “helloworld”, which when called returns a simple message containing a hello.
Create and run service locally¶
First step is to create a service called valuea.samples.helloworld which will return a simple dictionary when called.
1import valuea_framework.broker.Service
2
3
4class Service(valuea_framework.broker.Service.BaseService):
5 def execute(self):
6 return {'message': 'hello world!'}
7
Line 1 contains an import of the base class we’re extending from in line 4, this is a plain vanilla service, called “BaseService”.
We keep it simple, the only thing we do here is returning a message upon calling execute().
Note
Our class is called Service, this is a naming convention for services in the framework. Every file contains at most one service, which is named equally.
Next thing on our list is to try to run this service using only a python executable, which is what we’re going todo in the next block.
1#!/usr/bin/env python
2# tell python we want to use "helloworld"
3import services.valuea.samples.helloworld
4
5# construct a new object from the helloworld service
6srv = services.valuea.samples.helloworld.Service()
7
8# execute the service and return the result to a variable named result
9result = srv.execute()
10
11# put our data on screen as text
12print (result)
The emphasized lines contain the actual code, all starts with a directive to tell the shell we’er using a python interpreter on line 1 (not required) then we’re going to tell python we want to use the module “services.valuea.samples.helloworld”.
With the module included, we can construct a new helloworld service object, for which we can run the execute method.
Last thing on the list is to print our results on the screen, which returns something like:
{'message': 'hello world!'}
Expose service and test¶
This exercise consists of two parts, first we need to make sure there’s a listener running at our end to capture requests, next step is to publish a message (requesting our helloworld) to the queue and see if we can receive a result back.
1#!/usr/bin/env python
2import valuea_framework.broker.listener
3valuea_framework.broker.listener.run()
Note
The message broker code above can be used to start one or more processes depending on your configuration (broker.conf), we will use the same code for every example.
Tip
When using PyCharm, you can easily start the listener in your debugger and trap received events.
Now we’re listening for requests, we should be able to deliver a message in another session using standard RabbitMQ libraries. For python we’re using pika to do this, but to ease the process we’ve build a simple wrapper to do the actual call.
1#!/usr/bin/env python
2from valuea_framework.connectors.rabbitmq.Simple import SimpleRpcClient
3
4rpcclient = SimpleRpcClient(hostname='192.168.56.101',
5 exchange='default_exchange',
6 username='admin',
7 password='admin')
8
9
10remote_procedure = 'valuea.samples.helloworld'
11
12print(" [x] Requesting %s" % remote_procedure)
13response = rpcclient.call(remote_procedure)
14print(" [.] Got %r" % response)
The highlighted part in the above block constructs a new connection object, which connects to rabbitmq on the provided hostname/address using the same credentials created for testing.
Our next step is to define which procedure we want to call (valuea.samples.helloworld), telling the user we’re about to
execute (using a simple print) and perform the actual call using rpcclient.call(), then when we
receive a response, it will be printed to the console as well.
The output should look like this:
[x] Requesting valuea.samples.helloworld
[.] Got {u'message': u'hello world!'}
Note
The gui uses the same approach when calling services.