title: How to retrieve data from cloud (iothub)using mqtt in java
body: <p>I am new to IoTHub. I have successfully sent messages to IOT hub (D2C) using python.The protocol we used is mqtt.We are trying to retrieve data from cloud(IOT hub) using java,but could not able to find out a proper  way to recieve message from the cloud..My doubt is whether we can read messages from IOT Hub directly or we need to redirect the incoming messages to an event hub to retrieve the message.</p>
  
  <p>Also I tried to read messages from iothub in java simultaneously while sending data to cloud,but I got the error as follows..(Lost connection to the server. Reconnecting 0 time.)</p>
  
  <p>I use this code to read data from iothub,</p>
  
  <pre><code>import com.microsoft.azure.sdk.iot.device.DeviceClient;
  import com.microsoft.azure.sdk.iot.device.IotHubMessageResult;
  import com.microsoft.azure.sdk.iot.device.Message;
  import com.microsoft.azure.sdk.iot.device.MessageCallback;
  import com.microsoft.azure.sdk.iot.device.IotHubClientProtocol;
  import com.microsoft.azure.sdk.iot.service.sdk.IotHubServiceClientProtocol;
  import java.io.IOException;
  import java.net.URISyntaxException;
  import java.io.IOException;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  public class Kafkareception {
  
      public static void main(String[] args) throws IOException {
          try {
              String connString = "HostName=";
              IotHubClientProtocol protocol = IotHubClientProtocol.MQTT;
              DeviceClient client = new DeviceClient(connString, protocol);
  
              MessageCallback callback = new AppMessageCallback();
              client.setMessageCallback(callback, null);
              client.open();
          } catch (URISyntaxException ex) {
              Logger.getLogger(Kafkareception.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  
      private static class AppMessageCallback implements MessageCallback {
  
          public IotHubMessageResult execute(Message msg, Object context) {
              System.out.println(new String(msg.getBytes(), Message.DEFAULT_IOTHUB_MESSAGE_CHARSET) + "Received message from hub: ");
  
              return IotHubMessageResult.COMPLETE;
          }
      }
  }
  </code></pre>