In this example, we are going to create a Java proxy that gets the messages published by the example presented in the recipe Publishing messages from Android in the background and forward them to any mobile OS as seen in the recipe Binding an app from iPhone to RabbitMQ via MQTT, using an MQTT server as follows:
You need the Paho Java library client, which you can find at http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.java.git/.
amq.fanout
exchange:String myQueue = channel.queueDeclare().getQueue(); String myExchange = "amq.fanout"; channel.queueBind(myQueue,myExchange,"");
mqttClient = new MqttClient(MQTTip, MqttClient.generateClientId()); mqttClient.connect();
mqttClient.publish("follow.me", message, 0, false);
mosquitto_subscribe(mosq, NULL, "follow.me", 0);
We have to create two connections, one to RabbitMQ and one to the MQTT server; follow the steps 1-3.
When the Android app sends some messages, the consumer recieves and publishes them to the Mosquitto server using mqttClient.publish("follow.me", message, 0,false);
.
You need to change the topic on the iPhone app using follow.me
, and you also have to change the server's IP address.
The result is as shown in the following screenshot:
So, if the Android app is enabled, you can see the latitude and longitude in your iPhone app.
We have divided the broker's responsibility, one for the AMQP protocol and one for the MQTT protocol.
You can find more information about Paho at http://www.eclipse.org/paho/. When you build the Paho Java client source, the builder creates the Java doc in your filesystem: yourbasedir/org.eclipse.paho.mqtt.java/org.eclipse.paho.client.mqttv3/build/dist/javadoc
.