Crank It Up!¶
In this section, we describe using the AMPS samples from the command line. You can also use Eclipse or another IDE if you prefer.
To get started with AMPS, create a simple subscriber and publisher:
- Open a command shell
- Navigate to the directory where you unzipped the AMPS Java Client
- Navigate to
samples
Update the connection strings to connect to AMPS
FROM THE LINUX COMMAND LINE:
From the
samples
directory, type the following command to replace the IP address:$sed -ri 's/127.0.0.1/ip address here/' *.java
FROM THE WINDOWS COMMAND PROMPT:
From the
samples
directory, type the following command to replace the IP address:powershell "gci | foreach-object { (Get-Content $_) –replace '127.0.0.1' , 'ip address here' | Set-Content $_ }"
Build the samples. From the
samples
directory, type the following command to build the samples:$ ant build dist
The build.xml file in the
samples
directory contains targets for building and running the samples. For example, to build the console subscriber example, the equivalent command line to directly use the Java compiler is:$ javac -d bin -sourcepath src -cp ./lib/amps_client.jar \ ./src/com/crankuptheamps/examples/EX01AMPSConsoleSubscriber.java
Run the sample subscriber
From the
samples
directory, you can run the example by typing:$ ant runSub &
This starts the program
EX01AMPSConsoleSubscriber
, with source located atsrc/com/crankuptheamps/examples/EX01AMPSConsoleSubscriber.java
.
- The program connects to AMPS, subscribes to a topic, and waits for messages to arrive. When a message arrives, the program prints the message to the console.
Run the sample publisher
From the
samples
directory, run the publisher by typing:$ ant runPub &
This starts the program
EX02AMPSConsolePublisher
, with source located atsrc/com/crankuptheamps/examples/EX02AMPSConsolePublisher.java
.- The program runs, and subscribers receive the Hello, World message. The publisher itself produces no output if it is successful.
Using a State-of-the-World Database¶
One of the most useful parts of AMPS is being able to keep track of the current value of a message, using the state of the world (SOW) database:
Add messages to a state of the world database
From the
samples
directory, run the publisher by typing:$ ant runPubSOW &
This starts the program
EX03AMPSSOWConsolePublisher
, with source located atsrc/com/crankuptheamps/examples/EX03AMPSSOWConsolePublisher.java
.The program runs. This program sends 100 messages to AMPS. In this case, the messages are sent on a topic,
messages-sow
, that maintains a state-of-the-world database. AMPS saves the most recent version of each unique message. Unique messages are identified by the messageNumber in the message. The program sends messages 0-99, then updates message 5.
Query the state of the world
From the
samples
directory, run the query example by typing:$ ant runSOWandSubscribe &
This starts the program
EX05AMPSSOWandSubscribeConsoleSubscriber
, with source located atsrc/com/crankuptheamps/examples/EX05AMPSSOWandSubscribeConsoleSubscriber.java
.The program runs. This program queries the state of the world for the messages-sow topic, filtered to messages with a messageNumber less than 10. The console output shows the results of the query. Notice that for message 5, the database shows the latest value – the current state of the world. Rather than ending, the program continues to run. AMPS will send the program any new messages that match the filter.
Go beyond¶
The sample directory contains a number of sample programs, including programs that show how to use a bookmark subscription to replay messages published to a transaction log, programs that demonstrate publishing to and consuming from AMPS queues, programs that work with composite messages, and more.
You call the tune¶
Now that you’ve seen the samples run, it’s time for you to play with the samples. Modify them. Step through them in a debugger to see how they work.