Apache ActiveMQ 构建MQTT单消息服务器

构建消息服务器:

   服务器VMware ubuntu:

           Apache ActiveMQ

   win7客户端    :

      org.fusesource.mqtt-client  

      

   ubuntu 客户端

      paho.mqtt.c

      ./paho_c_sub topic --host (192.168.239.240) 服务器端的IP  --port  1883 --username admin --password password

 

 

win7 pub

 

package org.fusesource.mqtt;

/**
 * Created by Administrator on 2017/6/6.
 */


import org.fusesource.hawtbuf.Buffer;
import org.fusesource.hawtbuf.UTF8Buffer;
import org.fusesource.mqtt.client.FutureConnection;
import org.fusesource.mqtt.client.MQTT;
import org.fusesource.mqtt.client.QoS;


public class MQTTClient {

    public static void main(String[] args) throws Exception {
      // while (true) {
           //Thread.sleep(5000);
           sendMessage("hello");
       //}
    }
    public static void sendMessage(String message) {
        String user = env("APOLLO_USER", "admin");
        String password = env("APOLLO_PASSWORD", "admin");
        String host = env("APOLLO_HOST", "localhost");//apollo服务器地址
        host = "192.168.239.240";
        int port = 1883;//apollo端口号
       // String destination = "/topic/1/OOOOOOOOOO/aaaa";//topic

        String destination = "topic";//topic
        Buffer msg = new UTF8Buffer(message);
        MQTT mqtt = new MQTT();//新建MQTT
        try {
            mqtt.setHost(host, port);
            mqtt.setUserName(user);
            mqtt.setPassword(password);

            FutureConnection connection = mqtt.futureConnection();
            connection.connect().await();
            UTF8Buffer topic = new UTF8Buffer(destination);

            while (true) {
                Thread.sleep(4000);
                connection.publish(topic, msg, QoS.AT_LEAST_ONCE, false);
            }

           // connection.disconnect().await();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // System.exit(0);

        }

    }
    private static String env(String key, String defaultValue) {
        String rc = System.getenv(key);
        if (rc == null) {
            return defaultValue;
        }
        return rc;
    }
}


 

win7 SUB

 

package org.fusesource.mqtt;

/**
 * Created by Administrator on 2017/6/6.
 */
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 */


        import org.fusesource.hawtbuf.Buffer;
        import org.fusesource.hawtbuf.UTF8Buffer;
        import org.fusesource.mqtt.client.Callback;
        import org.fusesource.mqtt.client.CallbackConnection;
        import org.fusesource.mqtt.client.Listener;
        import org.fusesource.mqtt.client.MQTT;
        import org.fusesource.mqtt.client.QoS;
        import org.fusesource.mqtt.client.Topic;

/**
 * Uses an callback based interface to MQTT. Callback based interfaces are
 * harder to use but are slightly more efficient.
 */
class MQTTListener {
    public static void main(String[] args) throws Exception {

        String user = env("APOLLO_USER", "admin");
        String password = env("APOLLO_PASSWORD", "admin");
        //String host = env("APOLLO_HOST", "localhost");
        String host = env("APOLLO_HOST", "localhost");
        System.out.println("---------------:"+host.toString());
        host = "192.168.239.240";
        int port = Integer.parseInt(env("APOLLO_PORT", "1883"));
        //int port = Integer.parseInt(env("APOLLO_PORT", "61613"));
       // final String destination = arg(args, 0, "/topic/1/OOOOOOOOOO/aaaa");
        final String destination = arg(args, 0, "topic");

        MQTT mqtt = new MQTT();
        mqtt.setHost(host, port);
        mqtt.setUserName(user);
        mqtt.setPassword(password);
        mqtt.setKeepAlive((short) 30);
        // mqtt.setCleanSession(false);
        // mqtt.setClientId("aaaa");

        final CallbackConnection connection = mqtt.callbackConnection();

        connection.listener(new Listener() {
            public void onConnected() {
            }

            public void onDisconnected() {
            }

            public void onFailure(Throwable value) {
                value.printStackTrace();
                System.exit(-2);
            }

            public void onPublish(UTF8Buffer topic, Buffer msg, Runnable ack) {
                String body = msg.utf8().toString();
                System.out.println(body);
                ack.run();
            }
        });
        connection.connect(new Callback<Void>() {
            @Override
            public void onSuccess(Void value) {
                Topic[] topics = { new Topic(destination, QoS.AT_LEAST_ONCE) };
                connection.subscribe(topics, new Callback<byte[]>() {
                    public void onSuccess(byte[] qoses) {
                        System.out.println("connected...");
                    }

                    public void onFailure(Throwable value) {
                        value.printStackTrace();
                        System.exit(-2);
                    }
                });
            }

            @Override
            public void onFailure(Throwable value) {
                value.printStackTrace();
                System.exit(-2);
            }
        });
        // Wait forever..
        synchronized (MQTTListener.class) {
            while (true) {
                MQTTListener.class.wait();
            }
        }
    }

    private static String env(String key, String defaultValue) {
        String rc = System.getenv(key);
        if (rc == null) {
            return defaultValue;
        }
        return rc;
    }

    private static String arg(String[] args, int index, String defaultValue) {
        if (index < args.length) {
            return args[index];
        } else {
            return defaultValue;
        }
    }
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值