Domanda

I'm working on building my first maven plugin and am targeting maven 3. I am trying to understand how the instantiation strategy plays into the thread safety. If I keep the default "per-lookup" strategy, what does that mean? To me that means each execution of the plugin will instantiate a new mojo making it virtually impossible to write a non-thread safe plugin.

If I select the "singleton" strategy, I can see how thread safety issues would crop up since the same mojo could be in use at the same time within different threads causing the usual thread safety issues (i.e class level variables) to come into play.

In summary, does the per-lookup instantiation strategy work like I am thinking? What are some general rules of thumb/guidelines to make sure maven 3 plugins are actually thread safe (and not just marked as thread safe in the @Mojo annotation)?

È stato utile?

Soluzione

You are correct, per-lookup means that a new instance is created for every lookup from plexus. Each execution results in a lookup, so yes, for every execution a new instance is created.

It is thus the safest way to prevent multithreading issues.

However, you could still create multithreading issues by using non-final static fields.

So, use per-lookup and don't use static variables, and you are safe.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top