fusesource / mqtt-client

A Java MQTT Client
http://mqtt-client.fusesource.org/
Apache License 2.0
1.27k stars 368 forks source link

NullPointerException when disconnecting via CallbackConnection with CDI BeforeShutdownEvent #10

Open dobermai opened 12 years ago

dobermai commented 12 years ago

I use the mqtt-client library in a CDI Application and it works great, except when shutting down the application.

I implemented a shutdown Observer to disconnect gracefully when the application shuts down. This is the relevant code:

public class MQTTShutdownObserver implements Extension {

    public void observeShutdown(@Observes BeforeShutdown shutdown, MQTT mqtt) {
       //Note, the Connection is injected as Singleton
        System.out.println("Disconnecting from MQTT Broker");
        final CallbackConnection connection = new CallbackConnection(mqtt);
        connection.disconnect(new Callback<Void>() {
            @Override
            public void onSuccess(final Void value) {
               System.out.println("Successfully disconnected from MQTT Broker");
            }

            @Override
            public void onFailure(final Throwable value) {
                System.out.println("Error while disconnecting from MQTT Broker");
            }
        });
    }
}

Although I can gracefully disconnect, the CallbackConnection throws a NullPointerException on Line 501 when calling transport.flush. I think a check

if(transport != null){
transport.flush
}

should do the trick. I am very new to this library, so I am not sure if this would have any side effect?

If needed, here is the Stacktrace related to the bug:


ERROR [org.jboss.weld.Bootstrap] : java.lang.NullPointerException
    at org.fusesource.mqtt.client.CallbackConnection$8.onSuccess(CallbackConnection.java:501) [mqtt-client-1.2.jar:1.2]
    at org.fusesource.mqtt.client.CallbackConnection$8.onSuccess(CallbackConnection.java:490) [mqtt-client-1.2.jar:1.2]
    at org.fusesource.mqtt.client.CallbackConnection.disconnect(CallbackConnection.java:514) [mqtt-client-1.2.jar:1.2]
    at com.test.MyObserver.observeShutdown(MQTTShutdownObserver.java:21) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [classes.jar:1.6.0_33]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [classes.jar:1.6.0_33]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [classes.jar:1.6.0_33]
    at java.lang.reflect.Method.invoke(Method.java:597) [classes.jar:1.6.0_33]
    at org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:264) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:52) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:137) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:260) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.jboss.weld.introspector.jlr.WeldMethodImpl.invokeOnInstance(WeldMethodImpl.java:170) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.jboss.weld.introspector.ForwardingWeldMethod.invokeOnInstance(ForwardingWeldMethod.java:51) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:154) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:241) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:229) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:207) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.jboss.weld.bootstrap.events.AbstractContainerEvent.fire(AbstractContainerEvent.java:75) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.jboss.weld.bootstrap.events.BeforeShutdownImpl.fire(BeforeShutdownImpl.java:53) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.jboss.weld.bootstrap.events.BeforeShutdownImpl.fire(BeforeShutdownImpl.java:43) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.jboss.weld.bootstrap.WeldBootstrap.shutdown(WeldBootstrap.java:448) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.jboss.as.weld.WeldContainer.stop(WeldContainer.java:103) [jboss-as-weld-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.weld.services.WeldService.stop(WeldService.java:90) [jboss-as-weld-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StopTask.stopService(ServiceControllerImpl.java:1911)
    at org.jboss.msc.service.ServiceControllerImpl$StopTask.run(ServiceControllerImpl.java:1874)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [classes.jar:1.6.0_33]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [classes.jar:1.6.0_33]
    at java.lang.Thread.run(Thread.java:680) [classes.jar:1.6.0_33]