本文整理汇总了Java中org.fusesource.mqtt.client.MQTT.setHost方法的典型用法代码示例。如果您正苦于以下问题:Java MQTT.setHost方法的具体用法?Java MQTT.setHost怎么用?Java MQTT.setHost使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.fusesource.mqtt.client.MQTT
的用法示例。
在下文中一共展示了MQTT.setHost方法的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: 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();
}
示例4: newClient
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
private MQTT newClient(MqttLinkConfig config) {
MQTT client = new MQTT();
client.setClientId(config.getClientId());
client.setHost(URIs.newURI(connectionPrefix(config) + "://"
+ config.getHost() + ":" + config.getPort()));
String user = config.getUser();
if (!Strings.nullOrEmpty(user)) {
client.setUserName(user);
}
String password = config.getPassword();
if (!Strings.nullOrEmpty(password)) {
client.setPassword(password);
}
return client;
}
示例5: main
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
MQTT client = new MQTT();
client.setHost("tcp://raspberrypi.local:1883");
client.setClientId("fusesourcepublisher");
BlockingConnection connection = client.blockingConnection();
connection.connect();
connection.publish("testack", "Hello".getBytes(), QoS.AT_LEAST_ONCE, false);
while(true){
Thread.sleep(2000);
}
//connection.disconnect();
}
示例6: createMQTTSslConnection
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
private MQTT createMQTTSslConnection(String clientId, boolean clean) throws Exception {
MQTT mqtt = new MQTT();
mqtt.setConnectAttemptsMax(1);
mqtt.setReconnectAttemptsMax(0);
mqtt.setTracer(createTracer());
mqtt.setHost("ssl://localhost:" + port);
if (clientId != null) {
mqtt.setClientId(clientId);
}
mqtt.setCleanSession(clean);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0], new TrustManager[]{new DefaultTrustManager()}, new SecureRandom());
mqtt.setSslContext(ctx);
return mqtt;
}
示例7: start
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
@Start
public void start() throws URISyntaxException {
if (host != null && !host.isEmpty() && port != null) {
// create the MQTT client
client = new MQTT();
client.setHost(host, port);
connection = client.callbackConnection();
Log.info("{} is trying to connect to {}:{} ...", context.getInstanceName(), host, port);
connection.connect(new org.fusesource.mqtt.client.Callback<Void>() {
public void onFailure(Throwable throwable) {
Log.error(context.getInstanceName() + " failed to connect to " + host + ":" + port, throwable);
}
public void onSuccess(Void v) {
Log.info("{} connected to {}:{}", context.getInstanceName(), host, port);
}
});
}
}
示例8: 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();
}
}
示例9: 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();
}
}
示例10: 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);
}
}
示例11: mqttClient
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
protected static MQTT mqttClient(String host, int port) {
MQTT client = new MQTT();
client.setCleanSession(true);
client.setClientId("amc-" + Thread.currentThread().getId() + "-"
+ System.currentTimeMillis());
client.setHost(URIs.newURI("tcp://" + host + ":" + port));
return client;
}
示例12: 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);
}
}
示例13: retrieveMQTTConnection
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
private static BlockingConnection retrieveMQTTConnection(String host, String truststorePath, String truststorePass, String keystorePath, String keystorePass) throws Exception {
MQTT mqtt = new MQTT();
mqtt.setConnectAttemptsMax(0);
mqtt.setReconnectAttemptsMax(0);
mqtt.setHost(host);
mqtt.setSslContext(SSLSupport.createContext("JKS", keystorePath, keystorePass, "JKS", truststorePath, truststorePass));
mqtt.setCleanSession(true);
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
return connection;
}
示例14: main
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
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()));
}
示例15: retrieveMQTTConnection
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
private static BlockingConnection retrieveMQTTConnection(String host) throws Exception {
MQTT mqtt = new MQTT();
mqtt.setHost(host);
mqtt.setUserName("admin");
mqtt.setPassword("admin");
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
return connection;
}