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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import com.crankuptheamps.client.*;
 ...
 Client amps = new Client("myapplication");

 try
 {
       amps.connect("tcp://localhost:9007/amps/json");
       amps.logon();

       for (Message m : amps.subscribe("orders",
                                       "/symbol LIKE 'ABCD'"))
       {

         System.out.println(message.getData());

       }
     } catch (AMPSException exception) {
         exception.printStackTrace();
  }
  finally
  {
      amps.disconnect();
  }

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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
HAClient client = new HAClient("reconnectingSubscriber");

// The ServerChooser interface tells the HAClient which server
// to connect to, both for the initial connection and failover.
// The DefaultServerChooser is included with the client: many
// applications implement a ServerChooser to control failover
// behavior.

DefaultServerChooser sc = new DefaultServerChooser();
sc.add("tcp://amps-server:9007/amps/json");
sc.add("tcp://amps-failover-server:9007/amps/json");
client.setServerChooser(sc);

client.connectAndLogon();

// Enter subscriptions. If the connection to the server
// is lost, the HAClient will restore the connection to
// the server or the failover partner, and restore
// subscriptions.

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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class MyMessageHandler implements MessageHandler {
        HashMap<String, String> _data;
        public void invoke(Message m) {
          switch(m.getCommand()) {
          case Message.Command.OOF:
            _data.remove(m.getSowKey());
            break;
          case Message.Command.Publish:
            _data.put(m.getSowKey(), m.getData());
            break;
        }
      }

      Client amps = ...;
      amps.connect("tcp://localhost:9007/amps/json");
      amps.logon();

      amps.executeAsync( new Command("sow_and_subscribe").
                           setTopic("orders").
                           setFilter("/symbol LIKE 'ABC' " +
                                     "AND /qty > 100").
                           setOptions("oof"),
                           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 5.3.4.0, which supports AMPS servers version 5.3 and earlier.