DengerZone
DengerZone

Reputation: 3

AspectJ in multithreading

Currently im working on a application that runs the same process(with different input parameters in this case TaskInformation) parallelly on 16 threads. I would like to create Aspect from which i would take parameters and values and store them in a Object that i would create inside Aspect.

My question is this object which is created independent for every thread and can i somehow call the exact instance of the object so i can store right values for each thread.

I would avoid threadLocal if possible since this is something i currently use without aspect and i hope i can avoid it if possible.

Thanks for helping in advance!

Im using threadLocal and i would like to refactor this.

Edit: We have a Process class in which we are looking at process method, this method is running simultaneously in multiple threads. I would like to fill up the LoggingObject with information for every thread and at the end log them as a summary of the whole processing.

Example:

    public aspect MyAspect {

private final LoggingObject log = new LoggingObject()

        pointcut processMethodExecution(TaskInformation taskInfo):
        execution(TaskStatus Processor.process(TaskInformation)) && args(taskInfo);

        after(TaskInformation taskInfo): processMethodExecution(taskInfo) {
        logger.info("Processing task with ID: " + taskInfo.getTaskId());
        log.setID(taskInfo.getTaskId())
        }
        }

Upvotes: 0

Views: 238

Answers (1)

kriegaex
kriegaex

Reputation: 67457

You have several options:

  • Continue using the thread-local field, if it works for you. I see nothing wrong with it.

  • Replace the thread-local by a map with thread IDs as keys and your data as values. Be careful, however, if you use thread pools or so, because then threads will be re-used. Make sure to design your aspect in such a way that it initialises the data as expected. but actually, the same is true for a thread-local.

  • Select a non-singleton aspect instantiation model, as described in my answer here or, similarly, here. The answers also point to the AspectJ manual, which gives you more technical detail.

Upvotes: 0

Related Questions