LogoLogo
AMPS C#/.NET Client 5.3.3
AMPS C#/.NET Client 5.3.3
  • Welcome to the AMPS C#/.NET Client
    • Before You Start
    • Obtaining and Installing the AMPS C#/.NET Client
    • Your First AMPS Program
      • Client Identification
      • Connection Strings for AMPS
      • Connection Parameters for AMPS
      • Providing Credentials to AMPS
      • Assembly Deployment
    • Subscriptions
      • Content Filtering
        • Changing the Filter on a Subscription
      • Understanding Message Objects
      • Synchronous Message Processing
      • Asynchronous Message Processing
        • Understanding Threading
      • Regular Expression Subscriptions
      • Ending Subscriptions
    • Error Handling
      • Exceptions
      • Exception Types
      • Exception Handling and Asynchronous Message Processing
      • Controlling Blocking with Command Timeout
      • Disconnect Handling
        • Using a Heartbeat to Detect Disconnection
        • Managing Disconnection
        • Replacing Disconnect Handling
      • Unexpected Messages
      • Unhandled Exceptions
      • Detecting Write Failures
      • Monitoring Connection State
    • State of the World
      • SOW and Subscribe
      • Setting Batch Size
      • Managing SOW Contents
      • Client Side Conflation
    • Using Queues
      • Backlog and Smart Pipelining
      • Acknowledging Messages
      • Acknowledgement Batching
      • Returning a Message to the Queue
      • Manual Acknowledgement
    • Delta Publish and Subscribe
      • Delta Subscribe
      • Delta Publish
    • High Availability
    • AMPS Programming: Working with Commands
    • Utility Classes
    • Advanced Topics
    • Exceptions Reference
    • AMPS Server Documentation
    • API Documentation
Powered by GitBook

Get Help

  • FAQ
  • Legacy Documentation
  • Support / Contact Us

Get AMPS

  • Evaluate
  • Develop

60East Resources

  • Website
  • Privacy Policy

Copyright 2013-2024 60East Technologies, Inc.

On this page
Export as PDF
  1. Welcome to the AMPS C#/.NET Client

Subscriptions

Messages published to a topic on an AMPS server are available to other clients via a subscription. Before messages can be received, a client must subscribe to one or more topics on the AMPS server so that the server will begin sending messages to the client. The server will continue sending messages to the client until the client unsubscribes, or until the client disconnects. With content filtering, the AMPS server will limit the messages sent to only those messages that match a client-supplied filter. In this chapter, you will learn how to subscribe, unsubscribe, and supply filters for messages using the AMPS C# client.

Subscribing to a Topic

Subscribe to an AMPS topic by calling Client.subscribe(). Below is a short example (error handling and connection details are omitted for brevity):

class MyApp
{
    public static void Main()
    {

        // Here, we create a Client. We protect the Client in a using
        // block so that the connection and subscriptions are properly
        // cleaned up when dispose() is called.
        using(Client client = new Client("subscribe"))
        {

            client.connect("tcp://127.0.0.1/9007/amps");
            client.logon();


            // Here we subscribe to the topic messages. We do not provide
            // a filter, so the subscription receives all of the messages
            // published to the topic, regardless of content. The foreach
            // loop iterates over the messages returned by the MessageStream.
            // When we no longer need to subscribe, we can break out of the
            // loop. When the MessageStream is disposed, the client sends an
            // unsubscribe command to AMPS and stops receiving messages.
            foreach(Message m in client.subscribe("messages"))
            {

                // Within the loop, we process the message. In this case,
                // we simply print the contents of the message.
                System.Console.Writeline(m.getData());
            }
        }
    }
}

AMPS creates a background thread that receives the messages and copies them into the MessageStream that you iterate over. This means that the client application as a whole can continue to receive messages while you are doing processing work.

The simple method described above is provided for convenience. The AMPS C# client provides convenience methods for the most common forms of the commands. The client also provides an interface that gives you precise control over the command. Using that interface, the example above becomes:

class MyApp
{
    public static void Main()
    {

        // Here, we create a Client. We protect the Client in a using
        // block so that the connection and subscriptions are properly
        // cleaned up when dispose() is called.
        using(Client client = new Client("subscribe"))
        {
            client.connect("tcp://127.0.0.1/9007/amps");
            client.logon();

            // We create a Command object to subscribe to the messages topic.
            Command command = new Command("subscribe").setTopic("messages");

            // Here we execute the command and subscribe to the topic
            // messages. This works exactly the same way as the command
            // in the example above. We do not provide a filter, so the
            // subscription receives all of the messages published to the
            // topic, regardless of content.
            // The foreach loop iterates over the messages returned by
            // the MessageStream. When we no longer need to subscribe, we
            // can break out of the loop. When the MessageStream is disposed,
            // the client sends an unsubscribe command to AMPS and stops
            // receiving messages.
            foreach(Message m in client.execute(command))
            {
                // Within the loop, we process the message. In this case, we
                // simply print the contents of the message.
                System.Console.WriteLine(m.getData());
            }
        }
    }
}

The Command interface allows you to precisely customize the commands you send to AMPS. For flexibility and ease of maintenance, 60East recommends using the Command interface (rather than a named method) for any command that will receive messages from AMPS. For publishing messages, there can be a slight performance advantage to using the named commands where possible.

PreviousAssembly DeploymentNextContent Filtering

Last updated 2 months ago