1

Below is my initializer class in which I am creating multiple instance of Handler class:

@Singleton
public class Initializer {
  private static final Logger logger = Logger.getInstance(Initializer.class);
  private Handler handlerA;
  private Handler handlerB;
  private Handler handlerC;
  private Handler handlerD;
  // add another handler

  private Initializer() {

  }

  @PostConstruct
  public void postInit() {
    handlerA = new Handler(Type.A, 1);
    handlerB = new Handler(Type.B, 1);
    handlerC = new Handler(Type.C, 1);
    handlerD = new Handler(Type.D, 1);
    // add another handler instance
  }

  @PreDestroy
  public void shutdown() {
    handlerA.shutdown();
    handlerB.shutdown();
    handlerC.shutdown();
    handlerD.shutdown();
    // add another handler shutdown
  }
}

And below is my Handler constructor:

  public Handler(Type type, int poolSize) {
    this.executorService = Executors.newFixedThreadPool(poolSize);
    for (int i = 0; i < poolSize; i++) {
      Loop loop = HandlerFactory.getInstance().create(type);
      loops.add(loop);
      executorService.submit(loop);
    }
  }

My question is - If I need to add another handlerE in my Initializer class, then I need to add this line:

private Handler handlerE;

And then add another line in postInit() and shutdown() method. It looks ok but I was wondering if there is any better way to write that? If I have to add 10 more handlers, then it will be a single line for all those.

Wanted to see if there is any better way to write those Handlers in my Initializer class. I am working with Java7.

3
  • Have you one and one distinct Handler by Type ? Or a Type may have multiple Handlers ? Commented Feb 5, 2017 at 20:49
  • A type can have only one handler not multiple. Commented Feb 5, 2017 at 20:49
  • Ok. I will propose you an answer. Commented Feb 5, 2017 at 20:50

4 Answers 4

3

You could use of course a List to store the Handlers in the Initializer class.

But as you also search a way where the Initializer class would have less impact when Handlers are added (and deleted I assume too), I think that you should give more responsibility to the Type enum in order that the Initializer class delegate to them the Handler instantiation task.

As you said that you have one and only distinct Handler by Type, you could handle the configuration and the instantiation of the Handler in the side of the Type enum.
Each enum should have a constructor to set the required properties to create a Handler.
In this way, when you add/modify/delete a handler, you don't need to change the Initializer class.

Besides, you could also set the poolSize directly in the enum declaration and you can could enable or disable a Handler for a type as you suggest this need in a comment.

Here is the Type enum (I have disabled the Handler for the E value for the example):

public enum Type {

    A(1, true), B(1, true), C(1, true), D(1, true), E(1, false);

    private int poolSize;
    private boolean isHandlerEnabled;

    Type(int poolSize, boolean isHandlerEnabled) {
        this.poolSize = poolSize;
        this.isHandlerEnabled = isHandlerEnabled;
    }

    public int getPoolSize() {
        return poolSize;
    }

    public boolean isHandlerEnabled() {
        return isHandlerEnabled;
    }
    public createHandler(){
       if (!isHandlerEnabled){
           return null;
       }
       return new Handler(this, poolSize);
    }    
}

Here is the Initializer class:

public class `Initializer` {
    private List<Handler> handlers = new ArrayList<>();
    ...
    @PostConstruct
    public void postInit() {
       for (Type type : Type.values()){
          Handler handler = type.createHandler();
          if (handler != null){
             handlers.add(handler);
          }
       }                      
    }
    ...

    @PreDestroy
    public void shutdown() {
        for(Handler handler : handlers) {
            handler.shutdown();
        }
    }
 }
Sign up to request clarification or add additional context in comments.

Comments

2

Why not store them in a HashMap?

public class Initializer {
    private final Map<Type, Handler> handlers = new HashMap<>();
    ...
    @PostConstruct
    public void postInit() {
        handlers.put(Type.A, new Handler(Type.A, 1));
        handlers.put(Type.B, new Handler(Type.B, 1));
        handlers.put(Type.C, new Handler(Type.C, 1));
        handlers.put(Type.D, new Handler(Type.D, 1));
        handlers.put(Type.E, new Handler(Type.E, 1));
    }
    ...

    @PreDestroy
    public void shutdown() {
        for(Handler handler : handlers.values()) {
            handler.shutdown();
        }
    }

4 Comments

I am working with Java 7. Can you provide an example basis on that?
What is the goal of the map here ? A List could do the job.
@davidxxx I agree; Not knowing enough about how the class is used, and if a particular handler would need to be accessed, I just chose a container I thought would be useful. If there is no reason to lookup a handler then yes, I agree list would be better :^)
Sometimes we write a little fast our answers :)
1

Assuming Type is an enum and you want a Handler for each type, you can iterate over the values in the enum, create a new Handler and store the result in a List.

2 Comments

Yes type is an enum. Sometimes we comment out some handler in the initializer class (as we don't need while doing some testing) but generally we use all the handlers for each type.Can you provide an example how will I do that?
Well, I've walked through the algorithm for it. Is there a specific step you're struggling with? I'm trying not to turn this into a "gimme teh codez" question.
1

You can iterate over Type enum values like this.

 @PostConstruct
  public void postInit() {
    for (Type type : Type.values()) {
      handlersList.add(new Handler(type, 1));
    }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.