本文整理汇总了Java中org.fusesource.mqtt.client.QoS.AT_LEAST_ONCE属性的典型用法代码示例。如果您正苦于以下问题:Java QoS.AT_LEAST_ONCE属性的具体用法?Java QoS.AT_LEAST_ONCE怎么用?Java QoS.AT_LEAST_ONCE使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类org.fusesource.mqtt.client.QoS
的用法示例。
在下文中一共展示了QoS.AT_LEAST_ONCE属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
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
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: publish
public void publish(String queue, byte[] msg, QoS qos, Callback<Void> callback) {
if (qos == null) {
qos = QoS.AT_LEAST_ONCE;
}
if (callback == null) {
callback = new Callback<Void>() {
@Override
public void onSuccess(Void value) {
}
@Override
public void onFailure(Throwable value) {
LOGGER.info("Error sending message:", value);
}
};
}
connection.publish(queue, msg, qos, false, callback);
}
示例4: getQoS
static QoS getQoS(String qualityOfService) {
for (QoS q : QoS.values()) {
if (q.name().equalsIgnoreCase(qualityOfService)) {
return q;
}
}
if (qualityOfService.equalsIgnoreCase("ATMOSTONCE")) {
return QoS.AT_MOST_ONCE;
}
if (qualityOfService.equalsIgnoreCase("EXACTLYONCE")) {
return QoS.EXACTLY_ONCE;
}
if (qualityOfService.equalsIgnoreCase("ATLEASTONCE")) {
return QoS.AT_LEAST_ONCE;
}
throw new IllegalArgumentException("There is no QoS with name " + qualityOfService);
}
示例5: testClientDisconnectedOnMaxConsumerLimitReached
@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"));
}
示例6: testReferenceBinding
@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();
}
}
示例7: startMqttServerMock
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();
}
}
示例8: startMqttServerMockException
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();
}
}
示例9: TTNMqttClient
public TTNMqttClient(String broker, String appEUI, String accessKey, String devEUI) {
mGson = new GsonBuilder()
.registerTypeAdapter(DateTime.class, new DateTimeConverter())
.create();
mTopic = new Topic(appEUI + "/devices/" + devEUI + "/up", QoS.AT_LEAST_ONCE);
try {
mMqtt.setHost("tcp://" + broker + ":" + MQTT_HOST_PORT);
mMqtt.setUserName(appEUI);
mMqtt.setPassword(accessKey);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
示例10: main
public static void main(final String[] args) throws Exception {
// Create a new MQTT connection to the broker. We are not setting the client ID. The broker will pick one for us.
System.out.println("Connecting to Artemis using MQTT");
MQTT mqtt = new MQTT();
mqtt.setHost("tcp://localhost:1883");
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
System.out.println("Connected to Artemis");
// Subscribe to topics
Topic[] topics = {new Topic("mqtt/example/publish", QoS.AT_LEAST_ONCE), new Topic("test/#", QoS.EXACTLY_ONCE), new Topic("foo/+/bar", QoS.AT_LEAST_ONCE)};
connection.subscribe(topics);
System.out.println("Subscribed to topics.");
// Publish Messages
String payload1 = "This is message 1";
String payload2 = "This is message 2";
String payload3 = "This is message 3";
connection.publish("mqtt/example/publish", payload1.getBytes(), QoS.AT_LEAST_ONCE, false);
connection.publish("test/test", payload2.getBytes(), QoS.AT_MOST_ONCE, false);
connection.publish("foo/1/bar", payload3.getBytes(), QoS.AT_MOST_ONCE, false);
System.out.println("Sent messages.");
Message message1 = connection.receive(5, TimeUnit.SECONDS);
Message message2 = connection.receive(5, TimeUnit.SECONDS);
Message message3 = connection.receive(5, TimeUnit.SECONDS);
System.out.println("Received messages.");
System.out.println(new String(message1.getPayload()));
System.out.println(new String(message2.getPayload()));
System.out.println(new String(message3.getPayload()));
}
示例11: testLinkRouteAmqpReceiveMQTT
@Test(timeout = 60 * 1000)
public void testLinkRouteAmqpReceiveMQTT() throws Exception {
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("TestClient");
BlockingConnection blockingConnection = mqtt.blockingConnection();
blockingConnection.connect();
Topic t = new Topic("test", QoS.AT_LEAST_ONCE);
blockingConnection.subscribe(new Topic[]{t});
AmqpClient client = new AmqpClient(new URI(AMQP_URI), null, null);
AmqpConnection connection = client.connect();
try {
AmqpSession session = connection.createSession();
AmqpSender sender = session.createSender("test", true);
AmqpMessage message = new AmqpMessage();
message.setText("Test-Message");
sender.send(message);
sender.close();
} finally {
connection.close();
}
try {
blockingConnection.subscribe(new Topic[]{t});
assertNotNull(blockingConnection.receive(5, TimeUnit.SECONDS));
} finally {
blockingConnection.kill();
}
}
示例12: testSubscribeMultipleTopics
@Test(timeout = 30 * 10000)
public void testSubscribeMultipleTopics() throws Exception {
byte[] payload = new byte[1024 * 32];
for (int i = 0; i < payload.length; i++) {
payload[i] = '2';
}
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("MQTT-Client");
mqtt.setCleanSession(false);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
Topic[] topics = {new Topic("Topic/A", QoS.EXACTLY_ONCE), new Topic("Topic/B", QoS.EXACTLY_ONCE)};
Topic[] wildcardTopic = {new Topic("Topic/#", QoS.AT_LEAST_ONCE)};
connection.subscribe(wildcardTopic);
for (Topic topic : topics) {
connection.publish(topic.name().toString(), payload, QoS.AT_LEAST_ONCE, false);
}
int received = 0;
for (int i = 0; i < topics.length; ++i) {
Message message = connection.receive();
assertNotNull(message);
received++;
payload = message.getPayload();
String messageContent = new String(payload);
LOG.info("Received message from topic: " + message.getTopic() + " Message content: " + messageContent);
message.ack();
}
assertEquals("Should have received " + topics.length + " messages", topics.length, received);
}
示例13: testAnycastPrefixWorksWithMQTT
@Test(timeout = 60 * 1000)
public void testAnycastPrefixWorksWithMQTT() throws Exception {
String clientId = "testMqtt";
String anycastAddress = "anycast:foo/bar";
String sendAddress = "foo/bar";
Topic[] mqttSubscription = new Topic[]{new Topic(anycastAddress, QoS.AT_LEAST_ONCE)};
MQTT mqtt = createMQTTConnection();
mqtt.setClientId(clientId);
BlockingConnection connection1 = mqtt.blockingConnection();
connection1.connect();
connection1.subscribe(mqttSubscription);
MQTT mqtt2 = createMQTTConnection();
mqtt2.setClientId(clientId + "2");
BlockingConnection connection2 = mqtt2.blockingConnection();
connection2.connect();
connection2.subscribe(mqttSubscription);
String message1 = "TestMessage1";
String message2 = "TestMessage2";
connection1.publish(sendAddress, message1.getBytes(), QoS.AT_LEAST_ONCE, false);
connection2.publish(sendAddress, message2.getBytes(), QoS.AT_LEAST_ONCE, false);
assertNotNull(connection1.receive(1000, TimeUnit.MILLISECONDS));
assertNull(connection1.receive(1000, TimeUnit.MILLISECONDS));
assertNotNull(connection2.receive(1000, TimeUnit.MILLISECONDS));
assertNull(connection2.receive(1000, TimeUnit.MILLISECONDS));
}
示例14: testAnycastAddressWorksWithMQTT
@Test(timeout = 60 * 1000)
public void testAnycastAddressWorksWithMQTT() throws Exception {
String anycastAddress = "foo/bar";
getServer().addAddressInfo(new AddressInfo(SimpleString.toSimpleString("foo.bar"), RoutingType.ANYCAST));
String clientId = "testMqtt";
Topic[] mqttSubscription = new Topic[]{new Topic(anycastAddress, QoS.AT_LEAST_ONCE)};
MQTT mqtt = createMQTTConnection();
mqtt.setClientId(clientId);
BlockingConnection connection1 = mqtt.blockingConnection();
connection1.connect();
connection1.subscribe(mqttSubscription);
MQTT mqtt2 = createMQTTConnection();
mqtt2.setClientId(clientId + "2");
BlockingConnection connection2 = mqtt2.blockingConnection();
connection2.connect();
connection2.subscribe(mqttSubscription);
String message1 = "TestMessage1";
String message2 = "TestMessage2";
connection1.publish(anycastAddress, message1.getBytes(), QoS.AT_LEAST_ONCE, false);
connection2.publish(anycastAddress, message2.getBytes(), QoS.AT_LEAST_ONCE, false);
assertNotNull(connection1.receive(1000, TimeUnit.MILLISECONDS));
assertNull(connection1.receive(1000, TimeUnit.MILLISECONDS));
assertNotNull(connection2.receive(1000, TimeUnit.MILLISECONDS));
assertNull(connection2.receive(1000, TimeUnit.MILLISECONDS));
}
示例15: testAmbiguousRoutingWithMQTT
@Test(timeout = 60 * 1000)
public void testAmbiguousRoutingWithMQTT() throws Exception {
String anycastAddress = "foo/bar";
Set<RoutingType> routingTypeSet = new HashSet<>();
routingTypeSet.add(RoutingType.ANYCAST);
routingTypeSet.add(RoutingType.MULTICAST);
getServer().addAddressInfo(new AddressInfo(SimpleString.toSimpleString("foo.bar"), routingTypeSet));
String clientId = "testMqtt";
Topic[] mqttSubscription = new Topic[]{new Topic(anycastAddress, QoS.AT_LEAST_ONCE)};
MQTT mqtt = createMQTTConnection();
mqtt.setClientId(clientId);
BlockingConnection connection1 = mqtt.blockingConnection();
connection1.connect();
connection1.subscribe(mqttSubscription);
MQTT mqtt2 = createMQTTConnection();
mqtt2.setClientId(clientId + "2");
BlockingConnection connection2 = mqtt2.blockingConnection();
connection2.connect();
connection2.subscribe(mqttSubscription);
String message1 = "TestMessage1";
String message2 = "TestMessage2";
connection1.publish(anycastAddress, message1.getBytes(), QoS.AT_LEAST_ONCE, false);
connection2.publish(anycastAddress, message2.getBytes(), QoS.AT_LEAST_ONCE, false);
assertNotNull(connection1.receive(1000, TimeUnit.MILLISECONDS));
assertNotNull(connection1.receive(1000, TimeUnit.MILLISECONDS));
assertNotNull(connection2.receive(1000, TimeUnit.MILLISECONDS));
assertNotNull(connection2.receive(1000, TimeUnit.MILLISECONDS));
}