本文整理汇总了Java中org.fusesource.mqtt.client.MQTT.blockingConnection方法的典型用法代码示例。如果您正苦于以下问题:Java MQTT.blockingConnection方法的具体用法?Java MQTT.blockingConnection怎么用?Java MQTT.blockingConnection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.fusesource.mqtt.client.MQTT
的用法示例。
在下文中一共展示了MQTT.blockingConnection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的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.MQTT; //导入方法依赖的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: testConsumeMultipleTopicsWithWildcards
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
@Test
public void testConsumeMultipleTopicsWithWildcards() throws Exception {
MQTT mqtt = new MQTT();
BlockingConnection publisherConnection = mqtt.blockingConnection();
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMinimumMessageCount(numberOfMessages * (PUBLISH_TOPICS.length - 1));
publisherConnection.connect();
String payload;
for (String topic : PUBLISH_TOPICS) {
for (int i = 0; i < numberOfMessages; i++) {
payload = "Topic " + topic + ", Message " + i;
publisherConnection.publish(topic, payload.getBytes(), QoS.AT_LEAST_ONCE, false);
}
}
mock.await(5, TimeUnit.SECONDS);
mock.assertIsSatisfied();
}
示例4: canDetectTheMQTTProtocol
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
@Test// (timeout=60 * 1000)
public void canDetectTheMQTTProtocol() throws Exception {
DetectingGateway gateway = createGateway();
// Lets establish a connection....
MQTT mqtt = new MQTT();
mqtt.setHost("localhost", gateway.getBoundPort());
mqtt.setClientId("myclientid");
// mqtt.setVersion("3.1.1");
mqtt.setUserName("broker0/chirino");
mqtt.setConnectAttemptsMax(1);
org.fusesource.mqtt.client.BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
assertEquals(1, gateway.getSuccessfulConnectionAttempts());
assertEquals(1, gateway.getConnectedClients().length);
assertConnectedToBroker(0);
connection.kill();
}
示例5: testConsumeMultipleTopics
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的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.MQTT; //导入方法依赖的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: testTurnOffInactivityMonitor
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
@Test(timeout = 60 * 1000)
public void testTurnOffInactivityMonitor() throws Exception {
stopBroker();
protocolConfig = "transport.useInactivityMonitor=false";
startBroker();
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("foo3");
mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
assertTrue("KeepAlive didn't work properly", Wait.waitFor(() -> connection.isConnected()));
connection.disconnect();
}
示例8: testServiceBinding
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
@Test
public void testServiceBinding() throws Exception {
MockHandler mock = _testKit.replaceService("StoreReference");
MQTT mqtt = new MQTT();
BlockingConnection connection = mqtt.blockingConnection();
try {
connection.connect();
connection.publish(TOPIC_INPUT, MESSAGE_INPUT.getBytes(), QoS.AT_LEAST_ONCE, false);
Thread.sleep(1000);
LinkedBlockingQueue<Exchange> received = mock.getMessages();
Assert.assertNotNull(received);
Exchange exchange = received.iterator().next();
Assert.assertEquals(MESSAGE_OUTPUT, exchange.getMessage().getContent(String.class));
} finally {
connection.disconnect();
}
}
示例9: testConnectionWithNullPassword
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
@Test(timeout = 30000, expected = EOFException.class)
public void testConnectionWithNullPassword() throws Exception {
for (String version : Arrays.asList("3.1", "3.1.1")) {
BlockingConnection connection = null;
try {
MQTT mqtt = createMQTTConnection("test-" + version, true);
mqtt.setUserName(fullUser);
mqtt.setPassword((String) null);
mqtt.setConnectAttemptsMax(1);
mqtt.setVersion(version);
connection = mqtt.blockingConnection();
connection.connect();
fail("Connect should fail");
} finally {
if (connection != null && connection.isConnected()) connection.disconnect();
}
}
}
示例10: testConnectWithLargePassword
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
@Test
public void testConnectWithLargePassword() throws Exception {
for (String version : Arrays.asList("3.1", "3.1.1")) {
String longString = new String(new char[65535]);
BlockingConnection connection = null;
try {
MQTT mqtt = createMQTTConnection("test-" + version, true);
mqtt.setUserName(longString);
mqtt.setPassword(longString);
mqtt.setConnectAttemptsMax(1);
mqtt.setVersion(version);
connection = mqtt.blockingConnection();
connection.connect();
BlockingConnection finalConnection = connection;
assertTrue("Should be connected", Wait.waitFor(() -> finalConnection.isConnected(), 5000, 100));
} finally {
if (connection != null && connection.isConnected()) connection.disconnect();
}
}
}
示例11: startMqttServerMock
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的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();
}
}
示例12: startMqttServerMockException
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的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();
}
}
示例13: connect
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
public void connect() {
MQTT mqtt = new MQTT();
try {
mqtt.setHost(connectionInfo.getBiobrightUrl());
mqtt.setUserName(connectionInfo.getBiobrightUserName());
mqtt.setPassword(connectionInfo.getBiobrightPassword());
// TODO change security policy that is actually disabled with this code.
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
mqtt.setSslContext(sslContext);
logger.info("Opening MQTT socket.. ");
connection = mqtt.blockingConnection();
logger.info("Opened MQTT socket, connecting.. ");
connection.connect();
logger.info("Connected MQTT socket.. ");
} catch (Exception e) {
logger.error("connect()", e);
if(connection != null) {
connection = null;
}
throw new RuntimeException("Connection failed.", e);
}
}
示例14: testProduce
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
@Test
public void testProduce() throws Exception {
MQTT mqtt = new MQTT();
final BlockingConnection subscribeConnection = mqtt.blockingConnection();
subscribeConnection.connect();
Topic topic = new Topic(TEST_TOPIC, QoS.AT_MOST_ONCE);
Topic[] topics = {topic};
subscribeConnection.subscribe(topics);
final CountDownLatch latch = new CountDownLatch(numberOfMessages);
Thread thread = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < numberOfMessages; i++) {
try {
Message message = subscribeConnection.receive();
message.ack();
latch.countDown();
} catch (Exception e) {
e.printStackTrace();
break;
}
}
}
});
thread.start();
Producer producer = context.getEndpoint("direct:foo").createProducer();
for (int i = 0; i < numberOfMessages; i++) {
Exchange exchange = producer.createExchange();
exchange.getIn().setBody("test message " + i);
producer.process(exchange);
}
latch.await(20, TimeUnit.SECONDS);
assertTrue("Messages not consumed = " + latch.getCount(), latch.getCount() == 0);
}
示例15: setup
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
@Override
public void setup(OperatorContext context)
{
try {
client = new MQTT();
if (mqttClientConfig.getClientId() != null) {
client.setClientId(mqttClientConfig.getClientId());
}
client.setCleanSession(mqttClientConfig.isCleanSession());
client.setConnectAttemptsMax(mqttClientConfig.getConnectAttemptsMax());
client.setHost(mqttClientConfig.getHost(), mqttClientConfig.getPort());
client.setKeepAlive(mqttClientConfig.getKeepAliveInterval());
if (mqttClientConfig.getPassword() != null) {
client.setPassword(mqttClientConfig.getPassword());
}
if (mqttClientConfig.getUserName() != null) {
client.setUserName(mqttClientConfig.getUserName());
}
if (mqttClientConfig.getWillMessage() != null) {
client.setWillMessage(mqttClientConfig.getWillMessage());
client.setWillQos(mqttClientConfig.getWillQos());
client.setWillRetain(mqttClientConfig.isWillRetain());
client.setWillTopic(mqttClientConfig.getWillTopic());
}
connection = client.blockingConnection();
connection.connect();
} catch (Throwable t) {
throw new RuntimeException(t);
}
}