Inject a list of processes to execute using spring DI

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I have couple of MessageProcessors. One of the Processor will log the payload. Other Processor will save into an embedded database.



Here is the code I have written so far:



I am using annotation-driven configuration so I have omitted other classes which are not used in this use case.



@Component
public class ProcessorConfiguration
@Bean
@Qualifier("textMp")
public MessageProcessor textMessageProcessor()
return new TextMessageProcessor();


@Bean
@Qualifier("employMp")
public MessageProcessor employMessageProcessor()
return new EmployMessageProcessor();


@Bean
private List<MessageProcessor> messageProcessorList(@Qualifier("textMp") MessageProcessor textMessageProcessor,
@Qualifier("employMp") MessageProcessor employMessageProcessor)
List<MessageProcessor> list = new ArrayList<>();
list.add(textMessageProcessor);
list.add(employMessageProcessor);
return list;




This class is responsible for all the JMS messages the application receives.



public class MessageHandler 
@Autowired
private List<MessageProcessor> messageProcessors;

public void handleMessage(Notification notification)
messageProcessors.forEach(processor -> processor.doProcess(notification));



public interface MessageProcessor
void doProcess(Notification notification);


public class TextMessageProcessor implements MessageProcessor
private static final Logger logger = Logger.getLogger(TextMessageProcessor.class);
@Override
public void doProcess(Notification notification)
logger.info("The payload is " + notification.getText());





I have created a builder which takes String as input and returns an Employ object.



@Service
public class EmployMessageProcessor implements MessageProcessor
@Autowired
private EmployDao dao;

@Override
public void doProcess(Notification notification)
Employ employ = EmployBuilder.buildEmploy(notification.getText());
dao.save(employ);




public interface Notification
String getText();



I think, the way I am injecting the processors can be improved. Please review my code and provide your valuable feedback.







share|improve this question





















  • Does it work as expected so far?
    – Mast
    May 13 at 13:49










  • @Mast : Yes, I do not have any bugs so far.
    – NewUser
    May 13 at 14:00
















up vote
2
down vote

favorite












I have couple of MessageProcessors. One of the Processor will log the payload. Other Processor will save into an embedded database.



Here is the code I have written so far:



I am using annotation-driven configuration so I have omitted other classes which are not used in this use case.



@Component
public class ProcessorConfiguration
@Bean
@Qualifier("textMp")
public MessageProcessor textMessageProcessor()
return new TextMessageProcessor();


@Bean
@Qualifier("employMp")
public MessageProcessor employMessageProcessor()
return new EmployMessageProcessor();


@Bean
private List<MessageProcessor> messageProcessorList(@Qualifier("textMp") MessageProcessor textMessageProcessor,
@Qualifier("employMp") MessageProcessor employMessageProcessor)
List<MessageProcessor> list = new ArrayList<>();
list.add(textMessageProcessor);
list.add(employMessageProcessor);
return list;




This class is responsible for all the JMS messages the application receives.



public class MessageHandler 
@Autowired
private List<MessageProcessor> messageProcessors;

public void handleMessage(Notification notification)
messageProcessors.forEach(processor -> processor.doProcess(notification));



public interface MessageProcessor
void doProcess(Notification notification);


public class TextMessageProcessor implements MessageProcessor
private static final Logger logger = Logger.getLogger(TextMessageProcessor.class);
@Override
public void doProcess(Notification notification)
logger.info("The payload is " + notification.getText());





I have created a builder which takes String as input and returns an Employ object.



@Service
public class EmployMessageProcessor implements MessageProcessor
@Autowired
private EmployDao dao;

@Override
public void doProcess(Notification notification)
Employ employ = EmployBuilder.buildEmploy(notification.getText());
dao.save(employ);




public interface Notification
String getText();



I think, the way I am injecting the processors can be improved. Please review my code and provide your valuable feedback.







share|improve this question





















  • Does it work as expected so far?
    – Mast
    May 13 at 13:49










  • @Mast : Yes, I do not have any bugs so far.
    – NewUser
    May 13 at 14:00












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have couple of MessageProcessors. One of the Processor will log the payload. Other Processor will save into an embedded database.



Here is the code I have written so far:



I am using annotation-driven configuration so I have omitted other classes which are not used in this use case.



@Component
public class ProcessorConfiguration
@Bean
@Qualifier("textMp")
public MessageProcessor textMessageProcessor()
return new TextMessageProcessor();


@Bean
@Qualifier("employMp")
public MessageProcessor employMessageProcessor()
return new EmployMessageProcessor();


@Bean
private List<MessageProcessor> messageProcessorList(@Qualifier("textMp") MessageProcessor textMessageProcessor,
@Qualifier("employMp") MessageProcessor employMessageProcessor)
List<MessageProcessor> list = new ArrayList<>();
list.add(textMessageProcessor);
list.add(employMessageProcessor);
return list;




This class is responsible for all the JMS messages the application receives.



public class MessageHandler 
@Autowired
private List<MessageProcessor> messageProcessors;

public void handleMessage(Notification notification)
messageProcessors.forEach(processor -> processor.doProcess(notification));



public interface MessageProcessor
void doProcess(Notification notification);


public class TextMessageProcessor implements MessageProcessor
private static final Logger logger = Logger.getLogger(TextMessageProcessor.class);
@Override
public void doProcess(Notification notification)
logger.info("The payload is " + notification.getText());





I have created a builder which takes String as input and returns an Employ object.



@Service
public class EmployMessageProcessor implements MessageProcessor
@Autowired
private EmployDao dao;

@Override
public void doProcess(Notification notification)
Employ employ = EmployBuilder.buildEmploy(notification.getText());
dao.save(employ);




public interface Notification
String getText();



I think, the way I am injecting the processors can be improved. Please review my code and provide your valuable feedback.







share|improve this question













I have couple of MessageProcessors. One of the Processor will log the payload. Other Processor will save into an embedded database.



Here is the code I have written so far:



I am using annotation-driven configuration so I have omitted other classes which are not used in this use case.



@Component
public class ProcessorConfiguration
@Bean
@Qualifier("textMp")
public MessageProcessor textMessageProcessor()
return new TextMessageProcessor();


@Bean
@Qualifier("employMp")
public MessageProcessor employMessageProcessor()
return new EmployMessageProcessor();


@Bean
private List<MessageProcessor> messageProcessorList(@Qualifier("textMp") MessageProcessor textMessageProcessor,
@Qualifier("employMp") MessageProcessor employMessageProcessor)
List<MessageProcessor> list = new ArrayList<>();
list.add(textMessageProcessor);
list.add(employMessageProcessor);
return list;




This class is responsible for all the JMS messages the application receives.



public class MessageHandler 
@Autowired
private List<MessageProcessor> messageProcessors;

public void handleMessage(Notification notification)
messageProcessors.forEach(processor -> processor.doProcess(notification));



public interface MessageProcessor
void doProcess(Notification notification);


public class TextMessageProcessor implements MessageProcessor
private static final Logger logger = Logger.getLogger(TextMessageProcessor.class);
@Override
public void doProcess(Notification notification)
logger.info("The payload is " + notification.getText());





I have created a builder which takes String as input and returns an Employ object.



@Service
public class EmployMessageProcessor implements MessageProcessor
@Autowired
private EmployDao dao;

@Override
public void doProcess(Notification notification)
Employ employ = EmployBuilder.buildEmploy(notification.getText());
dao.save(employ);




public interface Notification
String getText();



I think, the way I am injecting the processors can be improved. Please review my code and provide your valuable feedback.









share|improve this question












share|improve this question




share|improve this question








edited May 13 at 13:48









Mast

7,32563484




7,32563484









asked May 13 at 13:40









NewUser

1154




1154











  • Does it work as expected so far?
    – Mast
    May 13 at 13:49










  • @Mast : Yes, I do not have any bugs so far.
    – NewUser
    May 13 at 14:00
















  • Does it work as expected so far?
    – Mast
    May 13 at 13:49










  • @Mast : Yes, I do not have any bugs so far.
    – NewUser
    May 13 at 14:00















Does it work as expected so far?
– Mast
May 13 at 13:49




Does it work as expected so far?
– Mast
May 13 at 13:49












@Mast : Yes, I do not have any bugs so far.
– NewUser
May 13 at 14:00




@Mast : Yes, I do not have any bugs so far.
– NewUser
May 13 at 14:00










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










This is a learning exercise for me as well so I hope I can be helpful with my answer.



  • I think you can loose the Configuration class entirely. Add
    @Component/@Service annotation to your TextMessageProcessor. Spring should be
    capable of figuring out MessageProcessor List wiring on its own. Have you already tried that?

If you are worried about the order of the beans inside MessageProcessor List then you can use @Order annotation on your Service and/or Component.



@Component
@Order(value=1)
public class TextMessageProcessor implements MessageProcessor



  • Your Configuration class in its current form is conflicting with Open Closed principle. In other words, whenever you add a new MessageProcessor, your configuration will grow. Imagine that configuration after you have 20 processors.



  • Spring creates qualifier names automatically as your bean names.
    From Spring documentation:




    For a fallback match, the bean name is considered as a default qualifier value.




  • I recommend doing your wiring through constructors instead of fields (I am especially eyeballing that DAO of yours). That way you preserve an ability to test your code later on. You will need to inject objects through constructors in your tests.


  • You could move Logging to MessageProcessor if it was an abstract class. Make a field or a method for Logging which returns the logging instance for current class. (Stackoverflow link)






share|improve this answer























  • Thanks for the feedback. I agree with all your points but, I am a bit confused with the feedback about Logging. I will do some homework on this and update. Appreciate your help, thanks.
    – NewUser
    May 14 at 10:54










  • With abstract class you can implement a functionality beforehand. So when that class is extended, it will inherit part of the functionality. You can do the same with interface default method as well though. The advantage of implementing the logging in the PARENT blueprint is that you could force yourself to log in every child element if you deem necessary. Also you would not repeat yourself. TLDR: you don't need to implement logging in children since parent does it for you.
    – Miko Nukka
    May 14 at 11:13











  • I happened to think over the advantages and disadvantages of moving logger to ParentClass. In the current approach, I have created one Logger instance per class as it is a static field. If I change to non-static, every object will have its own instance of logger which kinda freaks me out for the classes which serve most common requests in my application. It would be of great help if we get into a chat room and discuss your point of view on this? Please join this chat.stackexchange.com/rooms/77498/miko-nukka-dibya
    – NewUser
    May 15 at 6:56










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194310%2finject-a-list-of-processes-to-execute-using-spring-di%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










This is a learning exercise for me as well so I hope I can be helpful with my answer.



  • I think you can loose the Configuration class entirely. Add
    @Component/@Service annotation to your TextMessageProcessor. Spring should be
    capable of figuring out MessageProcessor List wiring on its own. Have you already tried that?

If you are worried about the order of the beans inside MessageProcessor List then you can use @Order annotation on your Service and/or Component.



@Component
@Order(value=1)
public class TextMessageProcessor implements MessageProcessor



  • Your Configuration class in its current form is conflicting with Open Closed principle. In other words, whenever you add a new MessageProcessor, your configuration will grow. Imagine that configuration after you have 20 processors.



  • Spring creates qualifier names automatically as your bean names.
    From Spring documentation:




    For a fallback match, the bean name is considered as a default qualifier value.




  • I recommend doing your wiring through constructors instead of fields (I am especially eyeballing that DAO of yours). That way you preserve an ability to test your code later on. You will need to inject objects through constructors in your tests.


  • You could move Logging to MessageProcessor if it was an abstract class. Make a field or a method for Logging which returns the logging instance for current class. (Stackoverflow link)






share|improve this answer























  • Thanks for the feedback. I agree with all your points but, I am a bit confused with the feedback about Logging. I will do some homework on this and update. Appreciate your help, thanks.
    – NewUser
    May 14 at 10:54










  • With abstract class you can implement a functionality beforehand. So when that class is extended, it will inherit part of the functionality. You can do the same with interface default method as well though. The advantage of implementing the logging in the PARENT blueprint is that you could force yourself to log in every child element if you deem necessary. Also you would not repeat yourself. TLDR: you don't need to implement logging in children since parent does it for you.
    – Miko Nukka
    May 14 at 11:13











  • I happened to think over the advantages and disadvantages of moving logger to ParentClass. In the current approach, I have created one Logger instance per class as it is a static field. If I change to non-static, every object will have its own instance of logger which kinda freaks me out for the classes which serve most common requests in my application. It would be of great help if we get into a chat room and discuss your point of view on this? Please join this chat.stackexchange.com/rooms/77498/miko-nukka-dibya
    – NewUser
    May 15 at 6:56














up vote
1
down vote



accepted










This is a learning exercise for me as well so I hope I can be helpful with my answer.



  • I think you can loose the Configuration class entirely. Add
    @Component/@Service annotation to your TextMessageProcessor. Spring should be
    capable of figuring out MessageProcessor List wiring on its own. Have you already tried that?

If you are worried about the order of the beans inside MessageProcessor List then you can use @Order annotation on your Service and/or Component.



@Component
@Order(value=1)
public class TextMessageProcessor implements MessageProcessor



  • Your Configuration class in its current form is conflicting with Open Closed principle. In other words, whenever you add a new MessageProcessor, your configuration will grow. Imagine that configuration after you have 20 processors.



  • Spring creates qualifier names automatically as your bean names.
    From Spring documentation:




    For a fallback match, the bean name is considered as a default qualifier value.




  • I recommend doing your wiring through constructors instead of fields (I am especially eyeballing that DAO of yours). That way you preserve an ability to test your code later on. You will need to inject objects through constructors in your tests.


  • You could move Logging to MessageProcessor if it was an abstract class. Make a field or a method for Logging which returns the logging instance for current class. (Stackoverflow link)






share|improve this answer























  • Thanks for the feedback. I agree with all your points but, I am a bit confused with the feedback about Logging. I will do some homework on this and update. Appreciate your help, thanks.
    – NewUser
    May 14 at 10:54










  • With abstract class you can implement a functionality beforehand. So when that class is extended, it will inherit part of the functionality. You can do the same with interface default method as well though. The advantage of implementing the logging in the PARENT blueprint is that you could force yourself to log in every child element if you deem necessary. Also you would not repeat yourself. TLDR: you don't need to implement logging in children since parent does it for you.
    – Miko Nukka
    May 14 at 11:13











  • I happened to think over the advantages and disadvantages of moving logger to ParentClass. In the current approach, I have created one Logger instance per class as it is a static field. If I change to non-static, every object will have its own instance of logger which kinda freaks me out for the classes which serve most common requests in my application. It would be of great help if we get into a chat room and discuss your point of view on this? Please join this chat.stackexchange.com/rooms/77498/miko-nukka-dibya
    – NewUser
    May 15 at 6:56












up vote
1
down vote



accepted







up vote
1
down vote



accepted






This is a learning exercise for me as well so I hope I can be helpful with my answer.



  • I think you can loose the Configuration class entirely. Add
    @Component/@Service annotation to your TextMessageProcessor. Spring should be
    capable of figuring out MessageProcessor List wiring on its own. Have you already tried that?

If you are worried about the order of the beans inside MessageProcessor List then you can use @Order annotation on your Service and/or Component.



@Component
@Order(value=1)
public class TextMessageProcessor implements MessageProcessor



  • Your Configuration class in its current form is conflicting with Open Closed principle. In other words, whenever you add a new MessageProcessor, your configuration will grow. Imagine that configuration after you have 20 processors.



  • Spring creates qualifier names automatically as your bean names.
    From Spring documentation:




    For a fallback match, the bean name is considered as a default qualifier value.




  • I recommend doing your wiring through constructors instead of fields (I am especially eyeballing that DAO of yours). That way you preserve an ability to test your code later on. You will need to inject objects through constructors in your tests.


  • You could move Logging to MessageProcessor if it was an abstract class. Make a field or a method for Logging which returns the logging instance for current class. (Stackoverflow link)






share|improve this answer















This is a learning exercise for me as well so I hope I can be helpful with my answer.



  • I think you can loose the Configuration class entirely. Add
    @Component/@Service annotation to your TextMessageProcessor. Spring should be
    capable of figuring out MessageProcessor List wiring on its own. Have you already tried that?

If you are worried about the order of the beans inside MessageProcessor List then you can use @Order annotation on your Service and/or Component.



@Component
@Order(value=1)
public class TextMessageProcessor implements MessageProcessor



  • Your Configuration class in its current form is conflicting with Open Closed principle. In other words, whenever you add a new MessageProcessor, your configuration will grow. Imagine that configuration after you have 20 processors.



  • Spring creates qualifier names automatically as your bean names.
    From Spring documentation:




    For a fallback match, the bean name is considered as a default qualifier value.




  • I recommend doing your wiring through constructors instead of fields (I am especially eyeballing that DAO of yours). That way you preserve an ability to test your code later on. You will need to inject objects through constructors in your tests.


  • You could move Logging to MessageProcessor if it was an abstract class. Make a field or a method for Logging which returns the logging instance for current class. (Stackoverflow link)







share|improve this answer















share|improve this answer



share|improve this answer








edited May 14 at 4:23









Mast

7,32563484




7,32563484











answered May 13 at 22:13









Miko Nukka

262




262











  • Thanks for the feedback. I agree with all your points but, I am a bit confused with the feedback about Logging. I will do some homework on this and update. Appreciate your help, thanks.
    – NewUser
    May 14 at 10:54










  • With abstract class you can implement a functionality beforehand. So when that class is extended, it will inherit part of the functionality. You can do the same with interface default method as well though. The advantage of implementing the logging in the PARENT blueprint is that you could force yourself to log in every child element if you deem necessary. Also you would not repeat yourself. TLDR: you don't need to implement logging in children since parent does it for you.
    – Miko Nukka
    May 14 at 11:13











  • I happened to think over the advantages and disadvantages of moving logger to ParentClass. In the current approach, I have created one Logger instance per class as it is a static field. If I change to non-static, every object will have its own instance of logger which kinda freaks me out for the classes which serve most common requests in my application. It would be of great help if we get into a chat room and discuss your point of view on this? Please join this chat.stackexchange.com/rooms/77498/miko-nukka-dibya
    – NewUser
    May 15 at 6:56
















  • Thanks for the feedback. I agree with all your points but, I am a bit confused with the feedback about Logging. I will do some homework on this and update. Appreciate your help, thanks.
    – NewUser
    May 14 at 10:54










  • With abstract class you can implement a functionality beforehand. So when that class is extended, it will inherit part of the functionality. You can do the same with interface default method as well though. The advantage of implementing the logging in the PARENT blueprint is that you could force yourself to log in every child element if you deem necessary. Also you would not repeat yourself. TLDR: you don't need to implement logging in children since parent does it for you.
    – Miko Nukka
    May 14 at 11:13











  • I happened to think over the advantages and disadvantages of moving logger to ParentClass. In the current approach, I have created one Logger instance per class as it is a static field. If I change to non-static, every object will have its own instance of logger which kinda freaks me out for the classes which serve most common requests in my application. It would be of great help if we get into a chat room and discuss your point of view on this? Please join this chat.stackexchange.com/rooms/77498/miko-nukka-dibya
    – NewUser
    May 15 at 6:56















Thanks for the feedback. I agree with all your points but, I am a bit confused with the feedback about Logging. I will do some homework on this and update. Appreciate your help, thanks.
– NewUser
May 14 at 10:54




Thanks for the feedback. I agree with all your points but, I am a bit confused with the feedback about Logging. I will do some homework on this and update. Appreciate your help, thanks.
– NewUser
May 14 at 10:54












With abstract class you can implement a functionality beforehand. So when that class is extended, it will inherit part of the functionality. You can do the same with interface default method as well though. The advantage of implementing the logging in the PARENT blueprint is that you could force yourself to log in every child element if you deem necessary. Also you would not repeat yourself. TLDR: you don't need to implement logging in children since parent does it for you.
– Miko Nukka
May 14 at 11:13





With abstract class you can implement a functionality beforehand. So when that class is extended, it will inherit part of the functionality. You can do the same with interface default method as well though. The advantage of implementing the logging in the PARENT blueprint is that you could force yourself to log in every child element if you deem necessary. Also you would not repeat yourself. TLDR: you don't need to implement logging in children since parent does it for you.
– Miko Nukka
May 14 at 11:13













I happened to think over the advantages and disadvantages of moving logger to ParentClass. In the current approach, I have created one Logger instance per class as it is a static field. If I change to non-static, every object will have its own instance of logger which kinda freaks me out for the classes which serve most common requests in my application. It would be of great help if we get into a chat room and discuss your point of view on this? Please join this chat.stackexchange.com/rooms/77498/miko-nukka-dibya
– NewUser
May 15 at 6:56




I happened to think over the advantages and disadvantages of moving logger to ParentClass. In the current approach, I have created one Logger instance per class as it is a static field. If I change to non-static, every object will have its own instance of logger which kinda freaks me out for the classes which serve most common requests in my application. It would be of great help if we get into a chat room and discuss your point of view on this? Please join this chat.stackexchange.com/rooms/77498/miko-nukka-dibya
– NewUser
May 15 at 6:56












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194310%2finject-a-list-of-processes-to-execute-using-spring-di%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?