Saturday, July 23, 2016

MQTT with SSL Connection Example

Using "setSSLProperties" in MqttConnectOptions we can easily configure SSL.
import java.util.Properties;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttMessage;

public class TestCon {
 public static void main(String[] args) {
  try {
   MqttClient client = new MqttClient("ssl://brocker_ip:8883", "testclient");
   MqttConnectOptions connectOptions = new MqttConnectOptions();
   connectOptions.setUserName("username");
   connectOptions.setPassword("password".toCharArray());
   Properties props = new Properties();
   props.setProperty("com.ibm.ssl.keyStore", "jksFilePath.jks");
   props.setProperty("com.ibm.ssl.keyStorePassword","jksPassword");
   connectOptions.setSSLProperties(props);
   client.connect(connectOptions);   
   MqttMessage  message = new MqttMessage();
   message.setPayload("test".getBytes());
   client.publish("topic", message);
   client.disconnect();   
  } catch (Exception e) {
   e.printStackTrace();
  }
  
 }
}

2 comments:

  1. Your blog has empowered me to embrace my passions and pursue my dreams with confidence – I'm thankful for the inspiration it provides! Challenge your friends to epic races in Unblocked Smash Karts - it's time to see who's the fastest on the track!

    ReplyDelete
  2. I admire how you make learning fun. Thank you for turning complex topics into approachable and enjoyable content that’s easy to understand and apply. Discover additional insights in this article. Experience Geometry Dash Free rhythm-based platforming where every jump syncs perfectly to the beat of engaging, dynamic music.

    ReplyDelete