samedi 18 février 2017

How to handle exception handling in singleton pattern?

I am trying to understand how to handle exception handling here. I have my below Singleton class which connects to cassandra, initialize all the metadata on the first call and then starts a periodic background thread which updates all our metadata every 15 minutes.

public class CassUtils {
  private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
  private final CassSession cassSession;

  private static class Holder {
    private static final CassUtils INSTANCE = new CassUtils();
  }

  public static CassUtils getInstance() {
    return Holder.INSTANCE;
  }

  private CassUtils() {
    CassConfig config = Utils.getConfig();
    try {
      this.cassSession = new CassSession(config);
      initializeMetadata(); // initializes metadata on the very first call
    } catch (Exception ex) {
      // log error
      throw new IllegalStateException("cannot initialize metadata: " + ex.getMessage());
    }
  }

  private void startScheduleTask() {
    scheduler.scheduleAtFixedRate(new Runnable() {
      public void run() {
        try {
          List<ProcessMetadata> processMetadata = getProcessMeta();
          List<ClientMetadata> clientMetadata = getClientMeta();
          List<ProcMetadata> procMetadata = getProcMeta();
          if (!processMetadata.isEmpty() && !clientMetadata.isEmpty())
            MetadataManager.setMetadata(processMetadata, clientMetadata, procMetadata);
        } catch (Exception ex) {
            // log error
        }
      }
    }, 30, 15, TimeUnit.MINUTES);
  }

  private void initializeMetadata() {
    List<ProcessMetadata> processMetadata = getProcessMeta(true);
    List<ClientMetadata> clientMetadata = getClientMeta();
    List<ProcMetadata> procMetadata = getProcMeta();
    if (processMetadata.isEmpty() || clientMetadata.isEmpty() || procMetadata.isEmpty()) {
      throw new IllegalStateException(); // is it ok to throw exception without any message here?
    }
    MetadataManager.setMetadata(processMetadata, clientMetadata, procMetadata);
    startScheduleTask();
  }

I need to notify the caller who is calling this singleton if there are any issues.

  • Now for whatever reason if CassSession throws exception because that is the class connects to cassandra and for some reason it is not able to connect to cassandra (mostly it will never happen), then I catch the exception in catch block in CassUtils class and throw IllegalStateException to the users with a message in it.
  • Also let's say if we are able to connect to cassandra through CassSession (which we will always) but in the initializeMetadata method, all our metadata are empty, then I don't want to proceed further so I am throwing IllegalStateException without any message in it. Is that ok? Because ultimately it will be caught by catch block of CassUtils class which is throwing exception to the calling users.

How to handle exception handling in singleton cases so that we can notify calling person whoever is calling this singleton. These above cases will happen during the first call at the initialization time. Is there any better way to do?

Aucun commentaire:

Enregistrer un commentaire