Stateful Services pattern
State variable bindings allow the svc_expr
service to act as a stateful
service, where values are persisted in the service instance.
Using service states
Procedure
Stateful Services pattern example
In the example in Table 1,
svc_expr
is used for initialising a state variable and incrementing it on each
execution of the request/reply.
Example | Description |
---|---|
<mix name="Mix"> <field name="count" type="integer"/> <field name="INCREMENT"/> <service name="Expr" provision="expr"> <prop name="expr.state" Count="NewCount"/> <prop name="expr.init" Count = "0"/> </service> <request name="Increment" service="Expr" fields="INCREMENT"> <prop name="expr.src" content-type="text/x-erlang"><![CDATA[ put("count", Count), NewCount = Count + 1, "Ok". ]]></prop> <reply name="Ok" fields="count"/> </request> </mix> |
The expr.state property declares that:
The service property The operation property
expr.src defines the value of the NewCount variable and
sends the reply "Ok" . The value of the count field is
populated using the put/2 function.Tip: An expression in
expr.src can also initialise a state variable. For example,
NewFoo = 0 . This leads to a state change, as opposed to using
expr.init , with the attribute
Foo="0" .Every time the operation is implemented,
it updates the service state with the value of Note: Do not manually type the
<![CDATA...]]> delimiters. The Editor
automatically renders text content as XML CDATA sections. |
Serializing Stateful Operations
The svc_expr
extension automatically serializes state
updates.
It does this by queuing all operations on the service such that only one operation at a time can execute, thus ensuring that state updates are atomic even if they affect several state variables per operation.
This queuing does not occur across operations on an svc_expr
service that has
no state variables defined.