public void onSuccess(Void value) { mqtt.tracer.debug("Restoring MQTT connection state"); // Setup a new overflow so that the replay can be sent out before the original overflow list. LinkedList<Request> originalOverflow = overflow; Map<Short, Request> originalRequests = requests; overflow = new LinkedList<Request>(); requests = new ConcurrentHashMap<Short, Request>(); // Restore any active subscriptions. if (!activeSubs.isEmpty()) { ArrayList<Topic> topics = new ArrayList<Topic>(activeSubs.size()); for (Map.Entry<UTF8Buffer, QoS> entry : activeSubs.entrySet()) { topics.add(new Topic(entry.getKey(), entry.getValue())); } send(new SUBSCRIBE().topics(topics.toArray(new Topic[topics.size()])), null); } // Replay any un-acked requests.. for (Map.Entry<Short, Request> entry : originalRequests.entrySet()) { MQTTFrame frame = entry.getValue().frame; frame.dup(true); // set the dup flag as these frames were previously transmitted. send(entry.getValue()); } // Replay the original overflow for (Request request : originalOverflow) { // Stuff in the overflow never got sent out.. so no need to set the dup flag send(request); } }
private void connectMqtt() throws Exception { String clientId = this.topologyName + "-" + this.context.getThisComponentId() + "-" + this.context.getThisTaskId(); MQTT client = MqttUtils.configureClient(this.options, clientId, this.keyStoreLoader); this.connection = client.callbackConnection(); this.connection.listener(this); this.connection.connect(new ConnectCallback()); while (!this.mqttConnected && !this.mqttConnectFailed) { LOG.info("Waiting for connection..."); Thread.sleep(500); } if (this.mqttConnected) { List<String> topicList = this.options.getTopics(); Topic[] topics = new Topic[topicList.size()]; QoS qos = MqttUtils.qosFromInt(this.options.getQos()); for (int i = 0; i < topicList.size(); i++) { topics[i] = new Topic(topicList.get(i), qos); } connection.subscribe(topics, new SubscribeCallback()); } }
public SUBSCRIBE decode(MQTTFrame frame) throws ProtocolException { assert(frame.buffers.length == 1); header(frame.header()); DataByteArrayInputStream is = new DataByteArrayInputStream(frame.buffers[0]); QoS qos = qos(); if(qos != QoS.AT_MOST_ONCE) { messageId = is.readShort(); } ArrayList<Topic> list = new ArrayList<Topic>(); while(is.available() > 0) { Topic topic = new Topic(MessageSupport.readUTF(is), QoS.values()[is.readByte()]); list.add(topic); } topics = list.toArray(new Topic[list.size()]); return this; }
main.topics.add(new Topic(shift(argl), qos)); } else { stderr("Invalid usage: unknown option: " + arg);
m_subscriber = m_mqtt.blockingConnection(); m_subscriber.connect(); Topic[] topics = new Topic[]{new Topic(willTestamentTopic, QoS.AT_MOST_ONCE)}; m_subscriber.subscribe(topics);
Forum f1 = new Forum(); Topic t1 = new Topic(); t1.forum = f1; t1.forum.someMethod();
Map<Long, Topic> topicsById = new HashMap<>(); while (rs.next()) { Long topicId = rs.getLong(1); String topicName = rs.getString(2); Long subTopicId = rs.getLong(3); String subTopicName = rs.getString(4); Topic topic = topicsById.get(topicId); if (topic == null) { topic = new Topic(topicId, topicName); topicsById.put(topicId, topic); } topic.addSubTopic(new SubTopic(subTopicId, subTopicName); } Collection<Topic> allTopics = topicsById.values();
@Override public void onSuccess(Void value) { if(size==1) { Topic[] Tp = new Topic[1]; Tp[0] = new Topic(topics, qos); connection.subscribe(Tp, cbs); System.out.println("Connect sucessfully with only one topic " + topics); } else if (size>1){ String[] topicArray = topics.split("\\s*,\\s*"); Topic[] Tp = new Topic[topicArray.length]; for (int i = 0; i < topicArray.length; i++) Tp[i] = new Topic(topicArray[i], qos); connection.subscribe(Tp, cbs); System.out.println("Connect sucessfully with these topics " + topics); } }
@Override protected CompletionStage<?> subscribeMqtt(final String topic, final MqttMessageHandler messageHandler) { synchronized (this) { subscriptions.put(topic, messageHandler); final CompletableFuture<byte[]> future = new CompletableFuture<>(); connection.subscribe( new org.fusesource.mqtt.client.Topic[] { new org.fusesource.mqtt.client.Topic(topic, QoS.AT_LEAST_ONCE) }, Callbacks.asCallback(future)); return future; } }
@Override protected CompletionStage<?> subscribeMqtt(final String topic, final MqttMessageHandler messageHandler) { synchronized (this) { subscriptions.put(topic, messageHandler); final CompletableFuture<byte[]> future = new CompletableFuture<>(); connection.subscribe( new org.fusesource.mqtt.client.Topic[] { new org.fusesource.mqtt.client.Topic(topic, QoS.AT_LEAST_ONCE) }, Callbacks.asCallback(future)); return future; } }
private void initializeConnection() throws Exception { connection = client.blockingConnection(); connection.connect(); if (!topicMap.isEmpty()) { Topic[] topics = new Topic[topicMap.size()]; int i = 0; for (Map.Entry<String, QoS> entry : topicMap.entrySet()) { topics[i++] = new Topic(entry.getKey(), entry.getValue()); } connection.subscribe(topics); } }
private void initializeConnection() throws Exception { connection = client.blockingConnection(); connection.connect(); if (!topicMap.isEmpty()) { Topic[] topics = new Topic[topicMap.size()]; int i = 0; for (Map.Entry<String, QoS> entry : topicMap.entrySet()) { topics[i++] = new Topic(entry.getKey(), entry.getValue()); } connection.subscribe(topics); } }
@Override public void onSuccess(Void e) { Topic[] topics = { new Topic(getTopic(), QoS.AT_LEAST_ONCE) }; connection.subscribe(topics, new Callback<byte[]>() { @Override public void onFailure(Throwable e) { LOGGER.error("MQTT subscribe failed.", e); } @Override public void onSuccess(byte[] arg0) { LOGGER.info("Subscribed to events on MQTT topic: " + getTopic()); } }); } });
@Override public void onSuccess(Void e) { Topic[] topics = { new Topic(getTopic(), QoS.AT_LEAST_ONCE) }; connection.subscribe(topics, new Callback<byte[]>() { @Override public void onFailure(Throwable e) { getLogger().error("MQTT subscribe failed.", e); } @Override public void onSuccess(byte[] arg0) { getLogger().info("Subscribed to events on MQTT topic: " + getTopic()); } }); } });
protected void restoreDurableSubs(List<SubscriptionInfo> subs) { try { for (SubscriptionInfo sub : subs) { String name = sub.getSubcriptionName(); String[] split = name.split(":", 2); QoS qoS = QoS.valueOf(split[0]); onSubscribe(new Topic(split[1], qoS)); // mark this durable subscription as restored by Broker restoredDurableSubs.add(MQTTProtocolSupport.convertMQTTToActiveMQ(split[1])); } } catch (IOException e) { LOG.warn("Could not restore the MQTT durable subs.", e); } }
protected void restoreDurableSubs(List<SubscriptionInfo> subs) { try { for (SubscriptionInfo sub : subs) { String name = sub.getSubcriptionName(); String[] split = name.split(":", 2); QoS qoS = QoS.valueOf(split[0]); onSubscribe(new Topic(split[1], qoS)); // mark this durable subscription as restored by Broker restoredDurableSubs.add(MQTTProtocolSupport.convertMQTTToActiveMQ(split[1])); } } catch (IOException e) { LOG.warn("Could not restore the MQTT durable subs.", e); } }
protected void restoreDurableSubs(List<SubscriptionInfo> subs) { try { for (SubscriptionInfo sub : subs) { String name = sub.getSubcriptionName(); String[] split = name.split(":", 2); QoS qoS = QoS.valueOf(split[0]); onSubscribe(new Topic(split[1], qoS)); // mark this durable subscription as restored by Broker restoredDurableSubs.add(MQTTProtocolSupport.convertMQTTToActiveMQ(split[1])); } } catch (IOException e) { LOG.warn("Could not restore the MQTT durable subs.", e); } }
public SUBSCRIBE decode(MQTTFrame frame) throws ProtocolException { assert(frame.buffers.length == 1); header(frame.header()); DataByteArrayInputStream is = new DataByteArrayInputStream(frame.buffers[0]); QoS qos = qos(); if(qos != QoS.AT_MOST_ONCE) { messageId = is.readShort(); } ArrayList<Topic> list = new ArrayList<Topic>(); while(is.available() > 0) { Topic topic = new Topic(MessageSupport.readUTF(is), QoS.values()[is.readByte()]); list.add(topic); } topics = list.toArray(new Topic[list.size()]); return this; }
@Override public void subscribe(String topic, int qos) throws Exception { Topic[] topics = {new Topic(utf8(topic), QoS.values()[qos])}; connection.subscribe(topics); }