Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

discovery: Use constructor injection to simplify lifecycle #835

Merged

Conversation

cweitkamp
Copy link
Contributor

  • Use constructor injection to simplify lifecycle

Fixes #834

Signed-off-by: Christoph Weitkamp [email protected]

this.thingRegistry = thingRegistry;
this.inbox = inbox;

// This should be the last step (to be more precise: providing the "this" reference to other ones as long as
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I do not understand from this comment, why these two lines couldn't be moved to the activate() method, which would seem to be the much better place to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can do that. But maybe I understood it wrong: Isn't the constructor part of the activation in this case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you added the @Activate annotation to the constructor, the constructor "is" the activate method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thus I conclude that the @Activate annotation is not mandatory for constructor injection? If so @kaikreuzer proposal sounds right.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the constructor "is" the activate method

I learned something new - thanks 🤗

Copy link
Contributor

@maggu2810 maggu2810 May 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, you understand my comment not correctly.
My comment has been about the code that is used by @cweitkamp (so, the reviewed one).

Here my understanding of the specification:
If constructor injection is used, the constructor must be annotated with @Activate.
You can add another activation method then you need to add another method that is annotated by @Activate.

There is an example here:
https://osgi.org/specification/osgi.cmpn/7.0.0/service.component.html#service.component-reference.element

So, the preferred solution here and for the other (IIRC it has been persistence inbox) would be to use the constructor injection (activate annotated constructor) and another activate method (activate annotated method).

    @Activate
    public AutomaticInboxProcessor(final @Reference ThingTypeRegistry thingTypeRegistry,
            final @Reference ThingRegistry thingRegistry, final @Reference Inbox inbox) {
        super(ThingStatusInfoChangedEvent.TYPE);
        this.thingTypeRegistry = thingTypeRegistry;
        this.thingRegistry = thingRegistry;
        this.inbox = inbox;
    }

    @Activate
    public void activate() {
        this.thingRegistry.addRegistryChangeListener(this);
        this.inbox.addInboxListener(this);
    }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, that is pretty confusing that the constructor requires the annotation as well, although it does not serve as the activation method... So then it would indeed be cleaner to add a dedicated activate() method for the listeners.

@kaikreuzer kaikreuzer merged commit 8b59505 into openhab:master May 27, 2019
@cweitkamp cweitkamp deleted the bugfix-834-automaticinboxprocessor-npe branch May 27, 2019 21:27
@wborn wborn added this to the 2.5 milestone Jul 30, 2019
Rosi2143 pushed a commit to Rosi2143/openhab-core that referenced this pull request Dec 26, 2020
splatch pushed a commit to ConnectorIO/copybara-hab-core that referenced this pull request Jul 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NPEs in 'AutomaticInboxProcessor' during startup
4 participants