How to Receive a Message from the IBM Message Hub with Java

MessageHubSample.java

import static java.lang.System.out;

import java.util.Arrays;
import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

public final class MessageHubSample {

  public static void main(final String[] args) {
    final Properties props = new Properties();

    props.put("bootstrap.servers", "nodename.messagehub.services.regionname.bluemix.net:9093");

    props.put("enable.auto.commit", "true");

    props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

    props.put("security.protocol", "SASL_SSL");

    props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

    final KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);

    consumer.subscribe(Arrays.asList("SampleTopic"));

    while (true) {
      final ConsumerRecords<String, String> records = consumer.poll(1000);

      for (final ConsumerRecord<String, String> record : records)
        out.println(record);

    }

  }

}

jaas.conf

KafkaClient {
  com.ibm.messagehub.login.MessageHubLoginModule required
  serviceName="kafka"
  username="username"
  password="password";
};

How to Send a Message to the IBM Message Hub with Java

MessageHubSample.java

import java.util.Properties;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

public final class MessageHubSample {

  public static void main(final String[] args) {
    final Properties props = new Properties();

    props.put("bootstrap.servers", "nodename.messagehub.services.regionname.bluemix.net:9093");

    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");

    props.put("security.protocol", "SASL_SSL");

    props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

    final Producer<String, String> producer = new KafkaProducer<>(props);

    producer.send(new ProducerRecord<String, String>("SampleTopic", "Sample Message"));

    producer.close();
  }

}

jaas.conf

KafkaClient {
  com.ibm.messagehub.login.MessageHubLoginModule required
  serviceName="kafka"
  username="username"
  password="password";
};

How to Receive a Message 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://localhost: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);
  }

}

How to Send a Message to IBM MQ Light with Java

import com.ibm.mqlight.api.ClientOptions;
import com.ibm.mqlight.api.NonBlockingClient;
import com.ibm.mqlight.api.NonBlockingClientAdapter;

public final class MqLightSample2 {

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

        public void onStarted(final NonBlockingClient client, final Void context) {
          client.send("SampleTopic", "Sample Message", null);
        }

      }, null);
  }

}

How to Receive a Message from a Microsoft Azure Service Bus Topic with Java

import static java.lang.System.out;

import com.microsoft.windowsazure.services.servicebus.ServiceBusConfiguration;
import com.microsoft.windowsazure.services.servicebus.ServiceBusContract;
import com.microsoft.windowsazure.services.servicebus.ServiceBusService;
import com.microsoft.windowsazure.services.servicebus.models.BrokeredMessage;
import com.microsoft.windowsazure.services.servicebus.models.ReceiveSubscriptionMessageResult;

public final class AzureSample {

  public static void main(final String[] args) throws Exception {
    final ServiceBusContract service = ServiceBusService.create(ServiceBusConfiguration.configureWithSASAuthentication("accountname", "sharedaccesskeyname", "sharedaccesskey", ".servicebus.windows.net"));

    final ReceiveSubscriptionMessageResult res = service.receiveSubscriptionMessage("SampleTopic", "SampleSubscription");

    final BrokeredMessage msg = res.getValue();

    out.println(msg);
  }

}

How to Send a Message to a Microsoft Azure Service Bus Topic with Java

import com.microsoft.windowsazure.services.servicebus.ServiceBusConfiguration;
import com.microsoft.windowsazure.services.servicebus.ServiceBusContract;
import com.microsoft.windowsazure.services.servicebus.ServiceBusService;
import com.microsoft.windowsazure.services.servicebus.models.BrokeredMessage;

public final class AzureSample {

  public static void main(final String[] args) throws Exception {
    final ServiceBusContract service = ServiceBusService.create(ServiceBusConfiguration.configureWithSASAuthentication("accountname", "sharedaccesskeyname", "sharedaccesskey", ".servicebus.windows.net"));

    service.sendMessage("SampleTopic", new BrokeredMessage("Simples Assim"));
  }

}

How to Receive a Message from a Microsoft Azure Service Bus Queue with Java

import static java.lang.System.out;

import com.microsoft.windowsazure.services.servicebus.ServiceBusConfiguration;
import com.microsoft.windowsazure.services.servicebus.ServiceBusContract;
import com.microsoft.windowsazure.services.servicebus.ServiceBusService;
import com.microsoft.windowsazure.services.servicebus.models.BrokeredMessage;
import com.microsoft.windowsazure.services.servicebus.models.ReceiveMessageResult;

public final class AzureSample {

  public static void main(final String[] args) throws Exception {
    final ServiceBusContract service = ServiceBusService.create(ServiceBusConfiguration.configureWithSASAuthentication("accountname", "sharedaccesskeyname", "sharedaccesskey", ".servicebus.windows.net"));

    final ReceiveMessageResult res = service.receiveMessage("SampleQueue");

    final BrokeredMessage msg = res.getValue();

    out.println(msg);
  }

}

How to Send a Message to a Microsoft Azure Service Bus Queue with Java

import com.microsoft.windowsazure.services.servicebus.ServiceBusConfiguration;
import com.microsoft.windowsazure.services.servicebus.ServiceBusContract;
import com.microsoft.windowsazure.services.servicebus.ServiceBusService;
import com.microsoft.windowsazure.services.servicebus.models.BrokeredMessage;

public final class AzureSample {

  public static void main(final String[] args) throws Exception {
    final ServiceBusContract service = ServiceBusService.create(ServiceBusConfiguration.configureWithSASAuthentication("accountname", "sharedaccesskeyname", "sharedaccesskey", ".servicebus.windows.net"));

    service.sendMessage("SampleQueue", new BrokeredMessage("Simples Assim"));
  }

}