Imagine, if you will, the following scenario. You have an AMPS SOW topic representing the state of orders in your business. You have a well tuned AMPS configuration, you have optimized your client applications and your network is at optimal capacity. You are processing millions of messages and everybody seems happy.
But you have discovered a nefarious problem. A consuming client is behind and there are orders that are not getting processed! Orders come into AMPS, but never leave the “Pending” state. Seconds go by and they still don’t update. They are stuck, like that last bit of jam that won’t come out of the jar. You don’t know which client is the culprit and you can’t keep track of which messages are being left behind!
What do you do? Hire a nanny to sit and watch your orders? Of course not!
You build a robot nanny!
AMPS to the rescue!
Action On Message Condition Timeout
Let me introduce you to one of the new, simple but powerful, actions included in AMPS 5.2. Action on Message Condition Timeout allows AMPS to run an action when a message in a SOW topic meets a specific condition for longer than a specified period of time.
This module uses the Out-of-Focus notification (OOF) mechanism to do its magic. When a message matches the specified topic and filter, the module begins tracking that message. If no OOF notification is received for that message within the specified timeout, the action runs for that message.
Back to our story, how do you save the day?
You use this action to configure amps to send an alert for messages in your orders SOW topic with a state of “Pending” and a duration of 5 seconds.
Any message that still has a state of “Pending” after 5 seconds will trigger the action.
Configure the action to publish a message to an “Alerts” topic, set up a “nanny-bot” to listen on the Alerts topic, and then boom!
Send an email, buzz your pager, turn on your toaster, or fire the death ray! You name it!
All at AMPS speed!
Working Example
Here are the steps to a basic working example that you can run on your own AMPS instance.
Note: This example assumes that you are running the AMPS server on your local machine using the loopback address. If you are running your AMPS server on a different machine or network configuration, the example may not work as written
1. Copy the following AMPS server configuration and save it to a file called sample-on-message-condition-timeout-config.xml
inside of your amps installation directory.
Note: This is a bare bones demo configuration. Use for anything else at your own risk!
<?xml version="1.0" encoding="UTF-8"?>
<AMPSConfig>
<!-- Name of the AMPS instance -->
<Name>AMPS-On-Message-Condition-Timeout-Demo</Name>
<!-- Configure the administrative HTTP server on port 8085
This HTTP server provides admin functions and statistics
for the instance
-->
<Admin>
<InetAddr>localhost:8085</InetAddr>
</Admin>
<!-- Configure a transport that accepts any known message type over
TCP port 9007 using the amps protocol. -->
<Transports>
<Transport>
<Name>any-tcp</Name>
<Type>tcp</Type>
<InetAddr>9007</InetAddr>
<Protocol>amps</Protocol>
</Transport>
</Transports>
<Logging>
<Target>
<Protocol>stdout</Protocol>
<Level>error</Level>
<IncludeErrors>00-0015</IncludeErrors>
</Target>
</Logging>
<SOW>
<Topic>
<Name>Orders</Name>
<MessageType>json</MessageType>
<Key>/OrderID</Key>
<Durability>transient</Durability>
</Topic>
<Topic>
<Name>Alerts</Name>
<MessageType>json</MessageType>
<Key>/OrderID</Key>
<Durability>transient</Durability>
</Topic>
</SOW>
<Actions>
<Action>
<On>
<Module>amps-action-on-message-condition-timeout</Module>
<Options>
<MessageType>json</MessageType>
<Topic>Orders</Topic>
<Filter>/status = 'PENDING'</Filter>
<Duration>5s</Duration>
</Options>
</On>
<Do>
<Module>amps-action-do-publish-message</Module>
<Options>
<MessageType>json</MessageType>
<Topic>Alerts</Topic>
<Data>{{AMPS_DATA}}</Data>
</Options>
</Do>
</Action>
</Actions>
</AMPSConfig>
2. Open a terminal and navigate to your amps installation directory.
3. Start amps using the configuration file that you just created:
bin/ampServer sample-on-message-condition-timeout-config.xml
4. Open a second terminal and navigate to your amps installation directory.
5. Run the following command to listen on the Alerts topic using spark, the AMPS reference client:
bin/spark subscribe -topic Alerts -server 127.0.0.1:9007/amps/json
6. Open a third terminal and navigate to your amps installation directory (last one, I promise).
7. Run the following command to send a message to the Orders topic using spark, the simple AMPS command-line client:
echo '{"id":1,"status":"PENDING","message":"Take over the world!"}' | bin/spark publish -topic Orders -server 127.0.0.1:9007/amps/json
8. Bring your second terminal into focus.
9. Wait 5 seconds; the longest time you have ever had to wait for anything AMPS related. Voila! Your message has appeared!
% bin/spark subscribe -topic Alerts -server 127.0.0.1:9007/amps/json
{"id":1,"status":"PENDING","message":"Take over the world!"}
I will leave the “firing of lasers” portion of the demo as an exercise to the user ;-)