How to Receive a Notification from IBM MQ Light with Java

import static com.ibm.mqlight.api.Delivery.Type.STRING;
import static java.lang.System.out;

import com.ibm.mqlight.api.ClientOptions;
import com.ibm.mqlight.api.Delivery;
import com.ibm.mqlight.api.DestinationAdapter;
import com.ibm.mqlight.api.NonBlockingClient;
import com.ibm.mqlight.api.NonBlockingClientAdapter;
import com.ibm.mqlight.api.StringDelivery;

public final class MqLightSample {

  public static void main(final String[] args) {
    NonBlockingClient.create("amqp://host:1912", ClientOptions.builder().setCredentials("username", "password").build(), new NonBlockingClientAdapter<Void>() {

        public void onStarted(final NonBlockingClient client, final Void context) {
          client.subscribe("SampleTopic", new DestinationAdapter<Void>() {

              public void onMessage(final NonBlockingClient client, final Void context, final Delivery delivery) {

                if (delivery.getType() == STRING)
                  out.println(((StringDelivery) delivery).getData());

              }

            }, null, null);
        }

      }, null);
  }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s