Java Client API

powerful. fast. easy.

The AMPS Java Client lets you write blazingly fast and cross-platform messaging applications in Java, using JDK 1.7 or greater. Here are a few Java examples to get you started:

Example 1: Connect and Subscribe

Connecting and subscibing to a content-filtered topic couldn't be easier:

 1 import com.crankuptheamps.client.*;
 2  ...
 3  Client amps = new Client("myapplication");
 5  try
 6  {
 7        amps.connect("tcp://localhost:9007/amps/json");
 8        amps.logon();
10        for (Message m : amps.subscribe("orders",
11                                        "/symbol LIKE 'ABCD'"))
12        {
14          System.out.println(message.getData());
16        }
17      } catch (AMPSException exception) {
18          exception.printStackTrace();
19   }
20   finally
21   {
22       amps.disconnect();
23   }

In this example, we connect to AMPS and subscribe to the "orders" topic. Messages are filtered on the server: we're only sent messages whose "symbol" field contains "ABCD". As messages arrive, they're handled by the invoke method of the anonymous inner class we provided, which prints messsage data to the console.

Example 2: Automatic Reconnection and Resubscription

Rock-solid applications must be able to recover from network outages. The AMPS Java client includes an HAClient class that includes automatic reconnection and resubscription. Best of all, easy-to-implement interfaces control reconnection and resubscription behavior, allowing you to easily customize failover.

 1 HAClient client = new HAClient("reconnectingSubscriber");
 3 // The ServerChooser interface tells the HAClient which server
 4 // to connect to, both for the initial connection and failover.
 5 // The DefaultServerChooser is included with the client: many
 6 // applications implement a ServerChooser to control failover
 7 // behavior.
 9 DefaultServerChooser sc = new DefaultServerChooser();
10 sc.add("tcp://amps-server:9007/amps/json");
11 sc.add("tcp://amps-failover-server:9007/amps/json");
12 client.setServerChooser(sc);
14 client.connectAndLogon();
16 // Enter subscriptions. If the connection to the server
17 // is lost, the HAClient will restore the connection to
18 // the server or the failover partner, and restore
19 // subscriptions.
21 client.subscribe(new MyMessageHandler(), "my-cool-topic", 0);

The HAClient class can, optionally, also provide store-and-forward for reliable publish. With the AMPS transaction log, the class can provide resumable subscriptions that are guaranteed not to miss messages or receive duplicate messages, even in the case of failover between replicated servers.

Example 3: Query the Contents of a "SOW" Topic

State-of-the-World ("SOW") topics in AMPS combine the power of a database table with the performance of a publish-subscribe system. Use the AMPS Java Client to query the contents of a SOW topic, and retrieve ongoing updates:

 1 public class MyMessageHandler implements MessageHandler {
 2         HashMap<String, String> _data;
 3         public void invoke(Message m) {
 4           switch(m.getCommand()) {
 5           case Message.Command.OOF:
 6             _data.remove(m.getSowKey());
 7             break;
 8           case Message.Command.Publish:
 9             _data.put(m.getSowKey(), m.getData());
10             break;
11         }
12       }
14       Client amps = ...;
15       amps.connect("tcp://localhost:9007/amps/json");
16       amps.logon();
18       amps.executeAsync( new Command("sow_and_subscribe").
19                            setTopic("orders").
20                            setFilter("/symbol LIKE 'ABC' " +
21                                      "AND /qty > 100").
22                            setOptions("oof"),
23                            new MyMessageHandler());

In this example, we keep a local HashMap up to date with the changes in an AMPS topic. Our message handler is called once for each row in the initial result set for our query of "orders." After that, it continues to be called each time any attribute of the order changes, with the Command set to Message.Command.Publish. When an order is no longer present in the orders topic, our handler is called with a message that has a Command of Message.Command.OOF.

Notice that, in this example, all of the processing occurs on a background thread. Many high performance applications receive messages on one thread and enqueue those messages to be processed by worker threads. The AMPS Java client includes an asynchronous interface that makes it easy to build high-performance multithreaded programs that follow this pattern.

Everything You Need

Ready to learn more? The AMPS Java Client includes everything you need to get started: libraries, sample applications, developer's guide, and reference documentation. Each AMPS Client includes complete source code so you can fully learn and understand AMPS, and best integrate it into your environment.

Click here to download and start using the AMPS Java Client. Once you've downloaded it, read the Developer Guide to get started.

If you need more help getting started, the 60East Technologies support and services team is ready to provide the support you need to help you CRANK UP THE AMPS.

The current release of the Java client is version, which supports AMPS servers version 5.3 and earlier.