BlockingQueue decoder that registers deleted objects

I have a BlockingQueue implementation that is used in a producer-consumer situation. I would like to decorate this queue so that every object that was taken from it is registered. I know what a simple implementation looks like: just implement a BlockingQueue and accept a BlockingQueue in the constructor to which all methods should be delegated. Is there any other way that I am missing? Perhaps a library? Is there something with the callback interface?

+4
source share
2 answers

An alternative that you might want to consider is dynamic proxies . This allows you to use the reflection style API to process requests made on this interface - it would be very simple to delegate all calls to the base implementation, adding some logging logic if the method name corresponded to one of the acceptance methods.

The disadvantage of this approach is that it adds a little extra overhead for all method calls (almost certainly negligible for general use, although this should be a yellow flag if used in a performance-critical section), and the code may end up looking cumbersome. Ultimately, what you do determines exactly the same behavior that you describe in your post, except that you do not need to explicitly write each delegation method, but provide a kind of wildcard implementation.

+1
source

I would have thought it would be easier to create a class that extends the corresponding implementation class for the BlockingQueue interface and overrides the remove method and others, if required.

EDIT

Wrapping is the best implementation if the OP uses more than one BlockingQueue implementation, but it introduces a slight performance hit for all operations and other minor issues.

My point is that extending a queue class is an alternative to wrapping it. Whether this is a better alternative depends on the circumstances.

+2
source

Source: https://habr.com/ru/post/1310468/


All Articles