本文整理匯總了Java中org.eclipse.paho.client.mqttv3.IMqttActionListener類的典型用法代碼示例。如果您正苦於以下問題:Java IMqttActionListener類的具體用法?Java IMqttActionListener怎麽用?Java IMqttActionListener使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
IMqttActionListener類屬於org.eclipse.paho.client.mqttv3包,在下文中一共展示了IMqttActionListener類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: whenCreateIsCalledThenAnObservableIsReturned
import org.eclipse.paho.client.mqttv3.IMqttActionListener; //導入依賴的package包/類
@Test
public void whenCreateIsCalledThenAnObservableIsReturned() throws Exception {
final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
final SubscribeFactory factory = new SubscribeFactory(client);
final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor.forClass(IMqttActionListener.class);
final ArgumentCaptor<IMqttMessageListener[]> messageListener = ArgumentCaptor.forClass(IMqttMessageListener[].class);
final String[] topics = new String[]{ "topic1", "topic2" };
final int[] qos = new int[]{ 1, 2 };
final Flowable<MqttMessage> obs = factory.create(topics, qos, BackpressureStrategy.ERROR);
Assert.assertNotNull(obs);
obs.subscribe();
Mockito.verify(client).subscribe(Mockito.same(topics),
Mockito.same(qos),
Mockito.isNull(),
actionListener.capture(),
messageListener.capture());
Assert.assertTrue(actionListener.getValue() instanceof SubscribeFactory.SubscribeActionListener);
Assert.assertTrue(messageListener.getValue() instanceof SubscriberMqttMessageListener[]);
Assert.assertEquals(2, messageListener.getValue().length);
}
示例2: whenCreateIsCalledAndAnErrorOccursThenObserverOnErrorIsCalled
import org.eclipse.paho.client.mqttv3.IMqttActionListener; //導入依賴的package包/類
@Test
public void whenCreateIsCalledAndAnErrorOccursThenObserverOnErrorIsCalled() throws Throwable {
expectedException.expectCause(isA(MqttException.class));
final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor.forClass(IMqttActionListener.class);
final ArgumentCaptor<IMqttMessageListener[]> messageListener = ArgumentCaptor.forClass(IMqttMessageListener[].class);
final String[] topics = new String[]{ "topic1", "topic2" };
final int[] qos = new int[]{ 1, 2 };
final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
Mockito.when(client.subscribe(Mockito.same(topics),
Mockito.same(qos),
Mockito.isNull(),
actionListener.capture(),
messageListener.capture()))
.thenThrow(new MqttException(MqttException.REASON_CODE_CLIENT_CONNECTED));
final SubscribeFactory factory = new SubscribeFactory(client);
final Flowable<MqttMessage> obs = factory.create(topics, qos, BackpressureStrategy.ERROR);
obs.blockingFirst();
}
示例3: whenCreateIsCalledThenAnObservableIsReturned
import org.eclipse.paho.client.mqttv3.IMqttActionListener; //導入依賴的package包/類
@Test
public void whenCreateIsCalledThenAnObservableIsReturned() throws Exception {
// Given
final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
final PublishFactory factory = new PublishFactory(client);
final String topic = "topic1";
final MqttMessage msg = MqttMessage.create(0, new byte[] { 'a', 'b', 'c' }, 1, true);
final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor.forClass(IMqttActionListener.class);
// When
final Single<PublishToken> obs = factory.create(topic, msg);
// Then
Assert.assertNotNull(obs);
obs.subscribe();
Mockito.verify(client).publish(Mockito.same(topic),
Mockito.same(msg.getPayload()), Mockito.anyInt(),
Mockito.anyBoolean(), Mockito.any(),
actionListener.capture());
Assert.assertTrue(actionListener.getValue() instanceof PublishFactory.PublishActionListener);
}
示例4: whenCreateIsCalledThenAnObservableIsReturned
import org.eclipse.paho.client.mqttv3.IMqttActionListener; //導入依賴的package包/類
@Test
public void whenCreateIsCalledThenAnObservableIsReturned() throws Exception {
// Given
final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
final UnsubscribeFactory factory = new UnsubscribeFactory(client);
final String[] topics = new String[]{ "topic1", "topic2" };
final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor.forClass(IMqttActionListener.class);
// When
final Completable obs = factory.create(topics);
// Then
Assert.assertNotNull(obs);
obs.subscribe();
Mockito.verify(client).unsubscribe(Mockito.same(topics), Mockito.isNull(),
actionListener.capture());
Assert.assertTrue(actionListener.getValue() instanceof UnsubscribeFactory.UnsubscribeActionListener);
}
示例5: whenCreateIsCalledThenAnObservableIsReturned
import org.eclipse.paho.client.mqttv3.IMqttActionListener; //導入依賴的package包/類
@Test
public void whenCreateIsCalledThenAnObservableIsReturned() throws Exception {
// Given
final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
final DisconnectFactory factory = new DisconnectFactory(client);
final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor.forClass(IMqttActionListener.class);
// When
final Completable obs = factory.create();
// Then
Assert.assertNotNull(obs);
obs.subscribe();
Mockito.verify(client).disconnect(Mockito.isNull(),
actionListener.capture());
Assert.assertTrue(actionListener.getValue() instanceof DisconnectFactory.DisconnectActionListener);
}
示例6: doConnect
import org.eclipse.paho.client.mqttv3.IMqttActionListener; //導入依賴的package包/類
/**
* Actually do the mqtt connect operation
*/
private void doConnect() {
if (clientHandle == null) {
clientHandle = mqttService.getClient(serverURI, clientId,myContext.getApplicationInfo().packageName,
persistence);
}
mqttService.setTraceEnabled(traceEnabled);
mqttService.setTraceCallbackId(clientHandle);
String activityToken = storeToken(connectToken);
try {
mqttService.connect(clientHandle, connectOptions, null,
activityToken);
}
catch (MqttException e) {
IMqttActionListener listener = connectToken.getActionCallback();
if (listener != null) {
listener.onFailure(connectToken, e);
}
}
}
示例7: subscribe
import org.eclipse.paho.client.mqttv3.IMqttActionListener; //導入依賴的package包/類
public void subscribe(String[] topicFilters, int[] qos, String invocationContext, String activityToken, IMqttMessageListener[] messageListeners) {
service.traceDebug(TAG, "subscribe({" + Arrays.toString(topicFilters) + "}," + Arrays.toString(qos) + ",{"
+ invocationContext + "}, {" + activityToken + "}");
final Bundle resultBundle = new Bundle();
resultBundle.putString(MqttServiceConstants.CALLBACK_ACTION, MqttServiceConstants.SUBSCRIBE_ACTION);
resultBundle.putString(MqttServiceConstants.CALLBACK_ACTIVITY_TOKEN, activityToken);
resultBundle.putString(MqttServiceConstants.CALLBACK_INVOCATION_CONTEXT, invocationContext);
if((myClient != null) && (myClient.isConnected())){
IMqttActionListener listener = new MqttConnectionListener(resultBundle);
try {
myClient.subscribe(topicFilters, qos,messageListeners);
} catch (Exception e){
handleException(resultBundle, e);
}
} else {
resultBundle.putString(MqttServiceConstants.CALLBACK_ERROR_MESSAGE, NOT_CONNECTED);
service.traceError("subscribe", NOT_CONNECTED);
service.callbackToActivity(clientHandle, Status.ERROR, resultBundle);
}
}
示例8: doConnect
import org.eclipse.paho.client.mqttv3.IMqttActionListener; //導入依賴的package包/類
/**
* Actually do the mqtt connect operation
*/
private void doConnect() {
if (clientHandle == null) {
clientHandle = mqttService.getClient(serverURI, clientId,String.valueOf(myContext.hashCode()),
persistence);
}
mqttService.setTraceEnabled(traceEnabled);
mqttService.setTraceCallbackId(clientHandle);
String activityToken = storeToken(connectToken);
try {
mqttService.connect(clientHandle, connectOptions, null,
activityToken);
}
catch (MqttException e) {
IMqttActionListener listener = connectToken.getActionCallback();
if (listener != null) {
listener.onFailure(connectToken, e);
}
}
}
示例9: doConnect
import org.eclipse.paho.client.mqttv3.IMqttActionListener; //導入依賴的package包/類
/**
* Actually do the mqtt connect operation
*/
private void doConnect() {
if (clientHandle == null) {
clientHandle = mqttService.getClient(serverURI, clientId,String.valueOf(myContext.hashCode()),
persistence);
}
mqttService.setTraceEnabled(traceEnabled);
mqttService.setTraceCallbackId(clientHandle);
String activityToken = storeToken(connectToken);
try {
mqttService.connect(clientHandle, connectOptions, null,
activityToken);
}
catch (MqttException e) {
IMqttActionListener listener = connectToken.getActionCallback();
if (listener != null) {
listener.onFailure(connectToken, e);
}
}
}
示例10: ConnectActionListener
import org.eclipse.paho.client.mqttv3.IMqttActionListener; //導入依賴的package包/類
/**
* @param persistence
* @param client
* @param comms
* @param options
* @param userToken
* @param userContext
* @param userCallback
*/
public ConnectActionListener(
MqttAsyncClient client,
MqttClientPersistence persistence,
ClientComms comms,
MqttConnectOptions options,
MqttToken userToken,
Object userContext,
IMqttActionListener userCallback) {
this.persistence = persistence;
this.client = client;
this.comms = comms;
this.options = options;
this.userToken = userToken;
this.userContext = userContext;
this.userCallback = userCallback;
this.originalMqttVersion = options.getMqttVersion();
}
示例11: fireActionEvent
import org.eclipse.paho.client.mqttv3.IMqttActionListener; //導入依賴的package包/類
/**
* An action has completed - if a completion listener has been set on the
* token then invoke it with the outcome of the action.
*
* @param token
*/
public void fireActionEvent(MqttToken token) {
final String methodName = "fireActionEvent";
if (token != null) {
IMqttActionListener asyncCB = token.getActionCallback();
if (asyncCB != null) {
if (token.getException() == null) {
// @TRACE 716=call onSuccess key={0}
log.fine(CLASS_NAME, methodName, "716",
new Object[] { token.internalTok.getKey() });
asyncCB.onSuccess(token);
} else {
// @TRACE 717=call onFailure key {0}
log.fine(CLASS_NAME, methodName, "716",
new Object[] { token.internalTok.getKey() });
asyncCB.onFailure(token, token.getException());
}
}
}
}
示例12: unsubscribeToTopic
import org.eclipse.paho.client.mqttv3.IMqttActionListener; //導入依賴的package包/類
public void unsubscribeToTopic(String topic, IMqttActionListener listener) {
try {
mqttAndroidClient.unsubscribe(topic, null, listener);
} catch (MqttException ex) {
System.err.println("Exception whilst subscribing");
ex.printStackTrace();
}
}
示例13: whenCreateIsCalledThenAnObservableIsReturned
import org.eclipse.paho.client.mqttv3.IMqttActionListener; //導入依賴的package包/類
@Test
public void whenCreateIsCalledThenAnObservableIsReturned() throws Exception {
final IMqttAsyncClient client = Mockito.mock(IMqttAsyncClient.class);
final MqttConnectOptions options = Mockito.mock(MqttConnectOptions.class);
final ConnectFactory factory = new ConnectFactory(client, options);
final ArgumentCaptor<IMqttActionListener> actionListener = ArgumentCaptor.forClass(IMqttActionListener.class);
final Completable obs = factory.create();
Assert.assertNotNull(obs);
obs.subscribe();
Mockito.verify(client).connect(Mockito.same(options), Mockito.isNull(),
actionListener.capture());
Assert.assertTrue(actionListener.getValue() instanceof ConnectFactory.ConnectActionListener);
}
示例14: doConnect
import org.eclipse.paho.client.mqttv3.IMqttActionListener; //導入依賴的package包/類
public void doConnect(IMqttActionListener listener) {
try {
//realiza a conexão com o broker
MqttConnectOptions options = new MqttConnectOptions();
options.setConnectionTimeout(5);
IMqttToken token = mqttAndroidClient.connect();
token.setActionCallback(listener);
} catch (MqttException e) {
e.printStackTrace();
}
}
示例15: subscribe
import org.eclipse.paho.client.mqttv3.IMqttActionListener; //導入依賴的package包/類
/**
* Subscribe to topic
*
* @param topic topic to subscribe
* @param qos define quality of service (check QosPolicy enum)
* @param listener completion listener (null allowed)
* @return
*/
protected void subscribe(String topic, QosPolicy qos, IMqttActionListener listener) {
if (isConnected()) {
try {
mClient.subscribe(topic, qos.getValue(), mContext, listener);
} catch (MqttException e) {
e.printStackTrace();
}
} else {
Log.e(TAG, "cant publish message. Not connected");
}
}