本文整理汇总了Java中org.fusesource.mqtt.client.Topic类的典型用法代码示例。如果您正苦于以下问题:Java Topic类的具体用法?Java Topic怎么用?Java Topic使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Topic类属于org.fusesource.mqtt.client包,在下文中一共展示了Topic类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
System.out.println("Connecting to Broker1 using MQTT");
MQTT mqtt = new MQTT();
mqtt.setHost(BROKER_URL);
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
System.out.println("Connected to Broker1");
// Subscribe to fidelityAds topic
Topic[] topics = { new Topic(FIDELITY_ADS_TOPIC, QoS.AT_LEAST_ONCE)};
connection.subscribe(topics);
// Publish Ads
String ads1 = "Discount on transfert fees up to -50% with coupon code JBOSSDOCTOR. www.beosbank.com";
long index=0;
while(true){
connection.publish(FIDELITY_ADS_TOPIC, (index+":"+ads1).getBytes(), QoS.AT_LEAST_ONCE, false);
System.out.println("Sent messages with index="+index);
Thread.sleep(10000);
index++;
}
}
示例2: main
import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
System.out.println("Connecting to Broker1 using MQTT");
MQTT mqtt = new MQTT();
mqtt.setHost(BROKER_URL);
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
System.out.println("Connected to Artemis");
// Subscribe to fidelityAds topic
Topic[] topics = {new Topic(FIDELITY_ADS_TOPIC, QoS.AT_LEAST_ONCE)};
connection.subscribe(topics);
// Get Ads Messages
while(true){
Message message = connection.receive(5, TimeUnit.SECONDS);
if(message!=null){
System.out.println("Received messages. "+new String(message.getPayload()));
}
}
}
示例3: connect
import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
public AnotherMqttClient connect() throws IOException {
connection = mqttClient.futureConnection();
exec(connection.connect());
new Thread() {
@Override
public void run() {
while (true) {
try {
org.fusesource.mqtt.client.Message message = exec(connection
.receive());
messages.add(new Message(message.getTopic(),
new String(message.getPayload())));
message.ack();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
exec(connection
.subscribe(new Topic[] { new Topic("#", AT_LEAST_ONCE) }));
return this;
}
示例4: start
import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
@Override
public boolean start() throws IOException {
LOG.debug("Starting MQTT reader ...");
Read spec = source.spec;
try {
client = spec.connectionConfiguration().createClient();
LOG.debug("Reader client ID is {}", client.getClientId());
checkpointMark.clientId = client.getClientId().toString();
connection = client.blockingConnection();
connection.connect();
connection.subscribe(new Topic[]{
new Topic(spec.connectionConfiguration().getTopic(), QoS.AT_LEAST_ONCE)});
return advance();
} catch (Exception e) {
throw new IOException(e);
}
}
示例5: testConsumeMultipleTopics
import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
@Test
public void testConsumeMultipleTopics() throws Exception {
MQTT mqtt = new MQTT();
BlockingConnection publisherConnection = mqtt.blockingConnection();
Topic topic1 = new Topic(TEST_TOPIC, QoS.AT_MOST_ONCE);
Topic topic2 = new Topic(TEST_TOPIC_2, QoS.AT_MOST_ONCE);
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMinimumMessageCount(numberOfMessages * 2);
publisherConnection.connect();
String payload;
for (int i = 0; i < numberOfMessages; i++) {
payload = "Topic 1, Message " + i;
publisherConnection.publish(topic1.name().toString(), payload.getBytes(), QoS.AT_LEAST_ONCE, false);
payload = "Topic 2, Message " + i;
publisherConnection.publish(topic2.name().toString(), payload.getBytes(), QoS.AT_LEAST_ONCE, false);
}
mock.await(5, TimeUnit.SECONDS);
mock.assertIsSatisfied();
}
示例6: testConsume
import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
@Test
public void testConsume() throws Exception {
MQTT mqtt = new MQTT();
BlockingConnection publisherConnection = mqtt.blockingConnection();
Topic topic = new Topic(TEST_TOPIC, QoS.AT_MOST_ONCE);
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMinimumMessageCount(numberOfMessages);
publisherConnection.connect();
for (int i = 0; i < numberOfMessages; i++) {
String payload = "Message " + i;
publisherConnection.publish(topic.name().toString(), payload.getBytes(), QoS.AT_LEAST_ONCE, false);
}
mock.await(5, TimeUnit.SECONDS);
mock.assertIsSatisfied();
}
示例7: run
import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
@Override
public void run()
{
try {
int i = 0;
Topic[] topics = new Topic[sendingData.size()];
for (String key : sendingData.keySet()) {
topics[i++] = new Topic(key, QoS.AT_MOST_ONCE);
}
connection.subscribe(topics);
while (receivedData.size() < sendingData.size()) {
Message msg = connection.receive();
receivedData.put(msg.getTopic(), new String(msg.getPayload()));
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
示例8: onSubscribe
import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
void onSubscribe(SUBSCRIBE command) throws MQTTProtocolException {
checkConnected();
Topic[] topics = command.topics();
if (topics != null) {
byte[] qos = new byte[topics.length];
for (int i = 0; i < topics.length; i++) {
qos[i] = (byte) onSubscribe(command, topics[i]).ordinal();
}
SUBACK ack = new SUBACK();
ack.messageId(command.messageId());
ack.grantedQos(qos);
try {
getMQTTTransport().sendToMQTT(ack.encode());
} catch (IOException e) {
LOG.warn("Couldn't send SUBACK for " + command, e);
}
} else {
LOG.warn("No topics defined for Subscription " + command);
}
}
示例9: callBroker
import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
private static void callBroker(String truststorePath, String truststorePass, String keystorePath, String keystorePass) throws Exception {
BlockingConnection connection = null;
try {
connection = retrieveMQTTConnection("ssl://localhost:1883", truststorePath, truststorePass, keystorePath, keystorePass);
// Subscribe to topics
Topic[] topics = {new Topic("test/+/some/#", QoS.AT_MOST_ONCE)};
connection.subscribe(topics);
// Publish Messages
String payload = "This is message 1";
connection.publish("test/1/some/la", payload.getBytes(), QoS.AT_LEAST_ONCE, false);
Message message = connection.receive(5, TimeUnit.SECONDS);
System.out.println("Message received: " + new String(message.getPayload()));
} catch (Exception e) {
throw e;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
示例10: doTestSendJMSReceiveMQTT
import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
public void doTestSendJMSReceiveMQTT(String destinationName) throws Exception {
final MQTTClientProvider provider = getMQTTClientProvider();
initializeConnection(provider);
provider.subscribe("foo/+", AT_MOST_ONCE);
Connection connection = cf.createConnection();
connection.start();
Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
javax.jms.Topic topic = s.createTopic(destinationName);
MessageProducer producer = s.createProducer(topic);
// send retained message from JMS
final byte[] bytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
BytesMessage bytesMessage = s.createBytesMessage();
bytesMessage.writeBytes(bytes);
producer.send(bytesMessage);
byte[] message = provider.receive(10000);
assertNotNull("Should get retained message", message);
assertArrayEquals(bytes, message);
provider.disconnect();
connection.close();
}
示例11: testClientDisconnectedOnMaxConsumerLimitReached
import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
@Test(timeout = 60 * 1000)
public void testClientDisconnectedOnMaxConsumerLimitReached() throws Exception {
Exception peerDisconnectedException = null;
try {
String clientId = "test.client";
SimpleString coreAddress = new SimpleString("foo.bar");
Topic[] mqttSubscription = new Topic[]{new Topic("foo/bar", QoS.AT_LEAST_ONCE)};
getServer().createQueue(coreAddress, RoutingType.MULTICAST, new SimpleString(clientId + "." + coreAddress), null, false, true, 0, false, true);
MQTT mqtt = createMQTTConnection();
mqtt.setClientId(clientId);
mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
connection.subscribe(mqttSubscription);
} catch (EOFException e) {
peerDisconnectedException = e;
}
assertNotNull(peerDisconnectedException);
assertTrue(peerDisconnectedException.getMessage().contains("Peer disconnected"));
}
示例12: testReferenceBinding
import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
@Test
public void testReferenceBinding() throws Exception {
MQTT mqtt = new MQTT();
Topic outputTopic = new Topic(TOPIC_OUTPUT, QoS.AT_LEAST_ONCE);
BlockingConnection connection = mqtt.blockingConnection();
try {
connection.connect();
connection.subscribe(new Topic[]{outputTopic});
_greet.sendInOnly(MESSAGE_INPUT);
Message message = connection.receive(1000, TimeUnit.MILLISECONDS);
Assert.assertNotNull("No output message from " + TOPIC_OUTPUT, message);
Assert.assertEquals(MESSAGE_OUTPUT, new String(message.getPayload()));
Assert.assertNull("More than one message received from " + TOPIC_OUTPUT,
connection.receive(1000, TimeUnit.MILLISECONDS));
} finally {
connection.disconnect();
}
}
示例13: startMqttServerMock
import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
private void startMqttServerMock() {
String broker = "tcp://appliance4.uniquid.co:1883";
String topic = "test";
Topic[] topics = {new Topic(topic, QoS.AT_LEAST_ONCE)};
BlockingConnection connection = null;
try{
MQTT mqtt = new MQTT();
mqtt.setHost(broker);
connection = mqtt.blockingConnection();
connection.connect();
connection.subscribe(topics);
// blocks!!!
Message message = connection.receive();
byte[] payload = message.getPayload();
message.ack();
Assert.assertNotNull(message);
FunctionRequestMessage rpcProviderRequest = (FunctionRequestMessage) new JSONMessageSerializer().deserialize(payload);
Assert.assertNotNull(rpcProviderRequest);
FunctionResponseMessage rpcProviderResponse = new FunctionResponseMessage();
rpcProviderResponse.setProvider("test");
rpcProviderResponse.setError(0);
rpcProviderResponse.setResult("result");
rpcProviderResponse.setId(rpcProviderRequest.getId());
connection.publish(rpcProviderRequest.getUser(), new JSONMessageSerializer().serialize(rpcProviderResponse), QoS.AT_LEAST_ONCE, false);
connection.disconnect();
} catch (Throwable t) {
Assert.fail();
}
}
示例14: startMqttServerMockException
import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
private void startMqttServerMockException() {
String broker = "tcp://appliance4.uniquid.co:1883";
String topic = "test";
Topic[] topics = {new Topic(topic, QoS.AT_LEAST_ONCE)};
BlockingConnection connection = null;
try{
MQTT mqtt = new MQTT();
mqtt.setHost(broker);
connection = mqtt.blockingConnection();
connection.connect();
connection.subscribe(topics);
// blocks!!!
Message message = connection.receive();
byte[] payload = message.getPayload();
message.ack();
Assert.assertNotNull(message);
FunctionRequestMessage functionRequestMessage = (FunctionRequestMessage) new JSONMessageSerializer().deserialize(payload);
Assert.assertNotNull(functionRequestMessage);
FunctionResponseMessage functionResponseMessage = new FunctionResponseMessage();
functionResponseMessage.setProvider("sender");
functionResponseMessage.setResult("result");
functionResponseMessage.setError(0);
connection.disconnect();
} catch (Throwable t) {
Assert.fail();
}
}
示例15: subscribe
import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
public void subscribe(String subscrTopic, QoS quality) {
Topic[] topics = { new Topic (subscrTopic, quality) };
connection.subscribe(topics, new Callback<byte[]>() {
public void onSuccess(byte[] qoses) { LOGGER.info("Subscribe success for topic {} with quality {}", subscrTopic, quality); }
public void onFailure(Throwable value) {
LOGGER.info("Subscribe failure for topic {}", subscrTopic, value);
}
});
}