Does the TMS MQTT work properly on the Windows service? Because I don't think so...

I have this problem with TMS MQTT, because the library does not work properly (does not connect to the broker) when used in a console application or Windows service.

Is it supposed to be like this or am I doing something wrong?

I'm really at a loss because I purchased this library with the idea of using it to transfer messages between microservices and here is such a "surprise".

I'll admit that I didn't check it thoroughly before purchasing it and I was suggested by the fact that you strongly emphasize that everything is thread-based.
And here there is clearly a problem with the creation of working threads when trying to connect to the broker.

Of course, in the "windows" application everything works as it should.

Internally, the MQTT client component uses threads. Not sure how this would prevent using it in a console or service app. We'll investigate.

Yes, I know there are used threads.
But I already know that thread synchronization for console applications is used incorrectly.

Please take a look:
https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Using_the_Main_VCL_Thread

And specifically this note:

Note: Because Synchronize uses a message loop, it does not work in console applications. For console applications, use other mechanisms, such as critical sections, to protect access to VCL objects.

And TMS MQTT simply uses the Synchornize method, and as far as I know, it has no right to work in a console application.

I temporarily solved this problem using the critical section, which I declared like this:

 TTMSMQTTWorkerThread = class abstract(TThread)
  private
    FConnection: TTMSMQTTNetworkConnection;
    FLogger: TTMSMQTTLogger;
    FSession: TTMSMQTTTMSMQTTClientSession;
    FRunInterval: integer;
    FPaused: Boolean;
    {$IFDEF CONSOLE}
      FCS : TCriticalSection;
    {$ENDIF}
  protected

And I use it like this:

 // Check if we need to reconnect
  if (not Session.HasActiveNetworkConnection) and FShouldTryReconnect then
  begin
    if (IncSecond(Session.LastWriteCommunicationTime, FReconnectInterval) <= LNow) then
    begin
      TTMSMQTTLogHelper.Info(Log, self, 'KEEP ALIVE - No Connection. Requesting Reconnect.');
        {$IFDEF CONSOLE}
          FCS.Enter;
          try
            RequestReconnect;
          finally
            FCS.Leave;
          end;
        {$ELSE}
          Synchronize(RequestReconnect);
        {$ENDIF}
      Session.LastWriteCommunicationTime := LNow;
    end;
  end;

Everything seems to work correctly, although I'm just starting more serious testing....

Is it possible to count on a fix?

1 Like

Can you check with the latest beta? We released an update today.
With this update, we have tested this and it works fine from a Windows service.
For use from a Windows service, please set the property SyncEvents to false.