redhat-developer / intellij-redhat-telemetry

IntelliJ Red Hat telemetry plugin
Eclipse Public License 2.0
9 stars 6 forks source link

Add sample with applicationService to create the telemetry singleton. #93

Open angelozerr opened 2 months ago

angelozerr commented 2 months ago

The sample at https://github.com/redhat-developer/intellij-redhat-telemetry?tab=readme-ov-file#create-a-telemetry-singleton suggest to create a class like this:

public class TelemetryService {

    private static final TelemetryService INSTANCE = new TelemetryService();

    private final Lazy<TelemetryMessageBuilder> builder = new Lazy<>(() -> new TelemetryMessageBuilder(TelemetryService.class.getClassLoader()));

    public static TelemetryMessageBuilder instance() {
        return INSTANCE.builder.get();
    }
}

If you see Jetbrains code, when they declare singleton, they use applicationService, see https://github.com/search?q=repo%3AJetBrains%2Fintellij-community%20getInstance&type=code

It would be nice to add the following sample code too:


package com.youapp.telemetry;

public class TelemetryService implements Disposable {

    private static final TelemetryService INSTANCE = new TelemetryService();

    private final Lazy<TelemetryMessageBuilder> builder = new Lazy<>(() -> new TelemetryMessageBuilder(TelemetryService.class.getClassLoader()));

    public static TelemetryMessageBuilder instance() {
        return getInstance().builder.get();
    }

   private static TelemetryService getInstance() {
        return ApplicationManager.getApplication().getService(TelemetryService.class);
    }

   @Override
   public void dispose() {
     // Do something when IJ IDEA is closing
   }

}

And declare in the plugin.xml the application service:

<applicationService
                serviceImplementation="com.youapp.telemetry.TelemetryService"/>
adietish commented 1 month ago

@angelozerr thanks for the suggestion. Very good idea, I'll do.