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

AMQP connection factory injection example improvement #1961

Closed
astefanutti opened this issue Jan 29, 2021 · 6 comments
Closed

AMQP connection factory injection example improvement #1961

astefanutti opened this issue Jan 29, 2021 · 6 comments

Comments

@astefanutti
Copy link
Member

astefanutti commented Jan 29, 2021

From the following example, taken from https://github.com/keunlee/camel-k-starter-workbench:

// camel-k: language=java property-file=application.properties
// camel-k: dependency=github:keunlee:camel-k-starter-workbench

import org.apache.camel.BindToRegistry;
import org.apache.camel.PropertyInject;
import org.apache.camel.builder.RouteBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.qpid.jms.JmsConnectionFactory;

public class HelloToAmqp extends RouteBuilder {
    private static final Logger LOG = LoggerFactory.getLogger(HelloToAmqp.class);

    @PropertyInject("messaging.broker.url.amqp")
    String messagingBrokerUrl;

    @BindToRegistry
    public JmsConnectionFactory connectionFactory() throws Exception {
        return new JmsConnectionFactory(messagingBrokerUrl);
    }

    @Override
    public void configure() throws Exception {
        from("timer:refresh?period=5000&fixedRate=true")
        .setBody()
        .simple("Hello World ${header.firedTime}")
        .log("${body}")
        .to("amqp:topic:example?exchangePattern=InOnly&connectionFactory=connectionFactory");
    }
}

It could be improved if:

  • Lookup by type of the connection factory would work, without having to rely on lookup by name
  • Remove the property injection and the connection factory method altogether, and simply pass quarkus.qpid-jms.url in the application.properties file, to leverage auto-configuration and lookup of the connection factory.
@ppalaga
Copy link

ppalaga commented Jan 29, 2021

@astefanutti is correct that the ConnectionFactory typically does not need to be created manually like in the snippet above. Passing quarkus.qpid-jms.url, quarkus.qpid-jms.username and quarkus.qpid-jms.password either via application.properties or via env vars/secrets QUARKUS_QPID_JMS_URL, etc. is simpler. See https://camel.apache.org/camel-quarkus/latest/reference/extensions/amqp.html#_additional_camel_quarkus_configuration and https://github.com/amqphub/quarkus-qpid-jms#configuration

If you really need to create the ConnectionFactory manually, then CDI and MicroProfile Config is the way to go on Quarkus:

// camel-k: language=java property-file=application.properties
// camel-k: dependency=github:keunlee:camel-k-starter-workbench

import org.apache.camel.BindToRegistry;
import org.apache.camel.PropertyInject;
import org.apache.camel.builder.RouteBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.qpid.jms.JmsConnectionFactory;

public class HelloToAmqp extends RouteBuilder {
    private static final Logger LOG = LoggerFactory.getLogger(HelloToAmqp.class);

    @org.eclipse.microprofile.config.inject.ConfigProperty("messaging.broker.url.amqp")
    String messagingBrokerUrl;

    @javax.enterprise.inject.Produces
    @javax.inject.Named("connectionFactory")
    public JmsConnectionFactory connectionFactory() throws Exception {
        return new JmsConnectionFactory(messagingBrokerUrl);
    }

    @Override
    public void configure() throws Exception {
        from("timer:refresh?period=5000&fixedRate=true")
        .setBody()
        .simple("Hello World ${header.firedTime}")
        .log("${body}")
        .to("amqp:topic:example?exchangePattern=InOnly&connectionFactory=connectionFactory");
    }
}

@lburgazzoli
Copy link
Contributor

lburgazzoli commented Jan 29, 2021

@ppalaga please be aware that cdi does not work in camel-k
maybe we could try to make it working

@ppalaga
Copy link

ppalaga commented Jan 29, 2021

@ppalaga please be aware that cdi does not work in camel-k

Oh

@lburgazzoli
Copy link
Contributor

lburgazzoli commented Feb 1, 2021

@astefanutti I think in the latest camel release lookup by type should work if the component option has the right annotations

@astefanutti
Copy link
Member Author

Thanks @lburgazzoli, I'll try it asap.

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale due to 90 days of inactivity.
It will be closed if no further activity occurs within 15 days.
If you think that’s incorrect or the issue should never stale, please simply write any comment.
Thanks for your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants