Injected constructor randomly called on startup

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Injected constructor randomly called on startup

Oscar Picasso-4
Hi,

I have written a very simple nexus plugin with an injected constructor.
It has a strange behavior. The injected constructor is only called randomly on startup.

When the constructor is not called during startup, the plugin does not work properly, and strangely enough the constructor is called during shutdown.

This can happen even if I restart nexus without changing the content of sonatype-work/nexus/plugin-repository.

Any idea?

Oscar
Reply | Threaded
Open this post in threaded view
|

Re: Injected constructor randomly called on startup

Stuart McCulloch-2
On 27 Mar 2014, at 14:12, Oscar Picasso <[hidden email]> wrote:

> Hi,
>
> I have written a very simple nexus plugin with an injected constructor.
> It has a strange behavior. The injected constructor is only called randomly on startup.

What interface does your class implement?  Components are only created when something else uses them (lazy creation), so it depends what you’re implementing and whether anything else uses it.

If you want to eagerly create a component when the plugin starts then add @EagerSingleton to the implementation class.

> When the constructor is not called during startup, the plugin does not work properly, and strangely enough the constructor is called during shutdown.

This could happen if something else is actively looking for components of type T during shutdown and your component implements T - it would then be created (lazily) so the caller can use it.

> This can happen even if I restart nexus without changing the content of sonatype-work/nexus/plugin-repository.
>
> Any idea?
>
> Oscar


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Injected constructor randomly called on startup

Oscar Picasso-4
Hi Stuart,

It implements EventSubscriber. 
I will try using @EagerSingleton

Thanks for your quick answer.

Oscar


On Thu, Mar 27, 2014 at 10:25 AM, Stuart McCulloch <[hidden email]> wrote:
On 27 Mar 2014, at 14:12, Oscar Picasso <[hidden email]> wrote:

> Hi,
>
> I have written a very simple nexus plugin with an injected constructor.
> It has a strange behavior. The injected constructor is only called randomly on startup.

What interface does your class implement?  Components are only created when something else uses them (lazy creation), so it depends what you’re implementing and whether anything else uses it.

If you want to eagerly create a component when the plugin starts then add @EagerSingleton to the implementation class.

> When the constructor is not called during startup, the plugin does not work properly, and strangely enough the constructor is called during shutdown.

This could happen if something else is actively looking for components of type T during shutdown and your component implements T - it would then be created (lazily) so the caller can use it.

> This can happen even if I restart nexus without changing the content of sonatype-work/nexus/plugin-repository.
>
> Any idea?
>
> Oscar


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]




--
blog: http://fp.opicasso.com
twitter: @oscarpica

Reply | Threaded
Open this post in threaded view
|

Re: Injected constructor randomly called on startup

Tamás Cservenák
Hm,

EventSubscribers are actively looked up by their "host": EventSubscriberHost.

Is your component maybe not a @Singleton?


On Thu, Mar 27, 2014 at 3:31 PM, Oscar Picasso <[hidden email]> wrote:
Hi Stuart,

It implements EventSubscriber. 
I will try using @EagerSingleton

Thanks for your quick answer.

Oscar


On Thu, Mar 27, 2014 at 10:25 AM, Stuart McCulloch <[hidden email]> wrote:
On 27 Mar 2014, at 14:12, Oscar Picasso <[hidden email]> wrote:

> Hi,
>
> I have written a very simple nexus plugin with an injected constructor.
> It has a strange behavior. The injected constructor is only called randomly on startup.

What interface does your class implement?  Components are only created when something else uses them (lazy creation), so it depends what you’re implementing and whether anything else uses it.

If you want to eagerly create a component when the plugin starts then add @EagerSingleton to the implementation class.

> When the constructor is not called during startup, the plugin does not work properly, and strangely enough the constructor is called during shutdown.

This could happen if something else is actively looking for components of type T during shutdown and your component implements T - it would then be created (lazily) so the caller can use it.

> This can happen even if I restart nexus without changing the content of sonatype-work/nexus/plugin-repository.
>
> Any idea?
>
> Oscar


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]




--
blog: http://fp.opicasso.com
twitter: @oscarpica


Reply | Threaded
Open this post in threaded view
|

Re: Injected constructor randomly called on startup

Oscar Picasso-4
Hi Tamás,

My component is a @Singleton.

Basically it's like the following:

[...]

@Named
@Singleton
public class DeployRecorder 
       extends ComponentSupport 
       implements EventSubscriber {

private final NexusAether nexusAether;
private final RepositoryRegistry repositoryRegistry;

@Inject
public DeployRecorder(final NexusAether nexusAether,
final RepositoryRegistry repositoryRegistry) {
log.info("Initializing Deploy Recorder...");
this.nexusAether = checkNotNull(nexusAether);
this.repositoryRegistry = checkNotNull(repositoryRegistry);
}

@Subscribe
public void onDeployEvent(RepositoryItemEventStoreCreate event) {
  [...]
}
}



On Thu, Mar 27, 2014 at 10:33 AM, Tamás Cservenák <[hidden email]> wrote:
Hm,

EventSubscribers are actively looked up by their "host": EventSubscriberHost.

Is your component maybe not a @Singleton?


On Thu, Mar 27, 2014 at 3:31 PM, Oscar Picasso <[hidden email]> wrote:
Hi Stuart,

It implements EventSubscriber. 
I will try using @EagerSingleton

Thanks for your quick answer.

Oscar


On Thu, Mar 27, 2014 at 10:25 AM, Stuart McCulloch <[hidden email]> wrote:
On 27 Mar 2014, at 14:12, Oscar Picasso <[hidden email]> wrote:

> Hi,
>
> I have written a very simple nexus plugin with an injected constructor.
> It has a strange behavior. The injected constructor is only called randomly on startup.

What interface does your class implement?  Components are only created when something else uses them (lazy creation), so it depends what you’re implementing and whether anything else uses it.

If you want to eagerly create a component when the plugin starts then add @EagerSingleton to the implementation class.

> When the constructor is not called during startup, the plugin does not work properly, and strangely enough the constructor is called during shutdown.

This could happen if something else is actively looking for components of type T during shutdown and your component implements T - it would then be created (lazily) so the caller can use it.

> This can happen even if I restart nexus without changing the content of sonatype-work/nexus/plugin-repository.
>
> Any idea?
>
> Oscar


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]




--
blog: http://fp.opicasso.com
twitter: @oscarpica





--
blog: http://fp.opicasso.com
twitter: @oscarpica

Reply | Threaded
Open this post in threaded view
|

Re: Injected constructor randomly called on startup

Stuart McCulloch-2
Hi Oscar,

I’ve put together a very basic plugin which has a dependency to the nexus-maven-bridge-plugin and contains an EventSubscriber like the one you posted below:


I built this plugin and installed it into Nexus as follows:

cd eventsub
mvn clean install
# stopped local nexus
unzip -d NEXUS_HOME/nexus/WEB-INF/plugin-repository target/eventsub-nexus-plugin-1.0-SNAPSHOT-bundle.zip
# restarted local nexus

the log showed the constructor was called during Nexus initialisation (just after the EventSubscriberHost started) and events were delivered as expected.

Note that when targeting Nexus releases after 2.7.x you should use nexus-plugin-bundle-maven-plugin 1.2 (this is also compatible with earlier Nexus releases)

Also when unpacking your plugin into the plugin-repository, remove any earlier version of the same plugin to make sure you are using the newly built files.

Hope this helps.

Cheers, Stuart

On 27 Mar 2014, at 14:50, Oscar Picasso <[hidden email]> wrote:

Hi Tamás,

My component is a @Singleton.

Basically it's like the following:

[...]

@Named
@Singleton
public class DeployRecorder 
       extends ComponentSupport 
       implements EventSubscriber {

private final NexusAether nexusAether;
private final RepositoryRegistry repositoryRegistry;

@Inject
public DeployRecorder(final NexusAether nexusAether,
final RepositoryRegistry repositoryRegistry) {
log.info("Initializing Deploy Recorder...");
this.nexusAether = checkNotNull(nexusAether);
this.repositoryRegistry = checkNotNull(repositoryRegistry);
}

@Subscribe
public void onDeployEvent(RepositoryItemEventStoreCreate event) {
  [...]
}
}



On Thu, Mar 27, 2014 at 10:33 AM, Tamás Cservenák <[hidden email]> wrote:
Hm,

EventSubscribers are actively looked up by their "host": EventSubscriberHost.

Is your component maybe not a @Singleton?


On Thu, Mar 27, 2014 at 3:31 PM, Oscar Picasso <[hidden email]> wrote:
Hi Stuart,

It implements EventSubscriber. 
I will try using @EagerSingleton

Thanks for your quick answer.

Oscar


On Thu, Mar 27, 2014 at 10:25 AM, Stuart McCulloch <[hidden email]> wrote:
On 27 Mar 2014, at 14:12, Oscar Picasso <[hidden email]> wrote:

> Hi,
>
> I have written a very simple nexus plugin with an injected constructor.
> It has a strange behavior. The injected constructor is only called randomly on startup.

What interface does your class implement?  Components are only created when something else uses them (lazy creation), so it depends what you’re implementing and whether anything else uses it.

If you want to eagerly create a component when the plugin starts then add @EagerSingleton to the implementation class.

> When the constructor is not called during startup, the plugin does not work properly, and strangely enough the constructor is called during shutdown.

This could happen if something else is actively looking for components of type T during shutdown and your component implements T - it would then be created (lazily) so the caller can use it.

> This can happen even if I restart nexus without changing the content of sonatype-work/nexus/plugin-repository.
>
> Any idea?
>
> Oscar


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]




--
blog: http://fp.opicasso.com
twitter: @oscarpica





--
blog: http://fp.opicasso.com
twitter: @oscarpica