Hi,
Yes, Riccardo.
//code snippet removing the route before recreating the authenticator bean
Route route = camelContext.getRoute(MyConstants.IMAP_ROUTE_ID);
if (route != null) {
camelContext.getRouteController().stopRoute(MyConstants.IMAP_ROUTE_ID);
camelContext.removeRoute(MyConstants.IMAP_ROUTE_ID);
}
camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from(getImapConfig(imapMailId, imapPort, pollingInterval, debugMode))
.routeId(MyConstants.IMAP_ROUTE_ID)
.autoStartup(true)
.to("direct:gotosecondstep");
}
});
//method where the route is being built as a string
public String getImapConfig(String imapUrl, String imapPort, String
pollingInterval, String debugMode) {
authenticatorService.updateAuthenticator();
return "imaps://" + imapUrl + ":" + imapPort
+ "?authenticator=#exchangeAuthenticator"
+ "&debugMode="+ debugMode
+ "&delete=false"
+ "&unseen=true"
+ "&delay=" + pollingInterval
+ "&mail.imaps.auth.mechanisms=XOAUTH2"
+ "&disconnect=true"
+ "&closeFolder=true";
}
Regards.
On Tue, Mar 4, 2025 at 4:22 PM Modanese, Riccardo
<[email protected]> wrote:
> Hi,
> I’ injecting beans at runtime using Camel with and without
> Spring (and it works) but, I did it before starting the route.
> From my understanding you are trying to replace a bean in the context once
> the route is running, am I right?
> If so, did you try to restart the route once you replace the bean in the
> context?
>
> Riccardo
>
> From: Dipak Rai <[email protected]>
> Date: Tuesday, 4 March 2025 at 11:47
> To: [email protected] <[email protected]>
> Subject: Re: Refreshing Bean To Build An IMAP Route Should Have Worked
> I'll rephrase my question here.
> Is it possible to just kick out the old bean from the Camel Context (and in
> turn from the Spring context) and inject a new bean while using the same to
> build a route dynamically as shown below? I don't see any explicit '
> *unbind()*' method with the *ApplicationContextBeanRepository*.
>
> *Code snippet:*
> public void updateAuthenticator() {
> String tenantId = emailConfigBean.getTenantId();
> String clientId = emailConfigBean.getClientId();
> String clientSecret = emailConfigBean.getClientSecret();
> String wsAdapterMailBox = emailConfigBean.getWSAdapterMailBox();
>
> try {
> MicrosoftExchangeOnlineOAuth2MailAuthenticator newExchangeAuthenticator =
> new MicrosoftExchangeOnlineOAuth2MailAuthenticator(
> tenantId, clientId, clientSecret, wsAdapterMailBox);
> Registry registry = camelContext.getRegistry(Registry.class);
> MicrosoftExchangeOnlineOAuth2MailAuthenticator existingAuthenticatorBean =
> registry.lookupByNameAndType("exchangeAuthenticator",
> MicrosoftExchangeOnlineOAuth2MailAuthenticator.class);
> // Unbind the existing 'exchangeAuthenticator' bean
> if (null != existingAuthenticatorBean) {
> registry.unbind("exchangeAuthenticator");
> }
> // Bind the new 'exchangeAuthenticator' bean
> registry.bind("exchangeAuthenticator", newExchangeAuthenticator);
> } catch (Exception e) {
> }
> }
>
>
> On Tue, Mar 4, 2025 at 10:40 AM Dipak Rai <[email protected]>
> wrote:
>
> > Hi,
> >
> > In the below snippet I am trying to dynamically build the IMAP route by
> > refreshing my 'exchangeAuthenticator' bean. Camel seemingly supports only
> > bean by name injection into the route. Although I am able to instantiate
> > the bean with new values yet the route refers to the old bean values and
> > fails with error.
> >
> > public void updateAuthenticator() {
> > String tenantId = emailConfigBean.getTenantId();
> > String clientId = emailConfigBean.getClientId();
> > String clientSecret = emailConfigBean.getClientSecret();
> > String wsAdapterMailBox = emailConfigBean.getWSAdapterMailBox();
> > try {
> > MicrosoftExchangeOnlineOAuth2MailAuthenticator
> newExchangeAuthenticator = new
> MicrosoftExchangeOnlineOAuth2MailAuthenticator(
> > tenantId, clientId, clientSecret, wsAdapterMailBox);
> > // Get the registry
> > DefaultRegistry defaultRegistry = (DefaultRegistry)
> camelContext.getRegistry();
> > // Unbind the existing 'exchangeAuthenticator' bean
> > MicrosoftExchangeOnlineOAuth2MailAuthenticator exchangeAuthenticator =
> (MicrosoftExchangeOnlineOAuth2MailAuthenticator) defaultRegistry
> > .lookupByName("exchangeAuthenticator");
> > if (null != exchangeAuthenticator) {
> > defaultRegistry.unbind("exchangeAuthenticator");
> > }
> > defaultRegistry.bind("exchangeAuthenticator",
> newExchangeAuthenticator);
> > defaultRegistry.setCamelContext(camelContext);
> > } catch (Exception e) {
> > log.error("Exception in updating authenticator bean: {}",
> e.getMessage());
> > }
> > }
> >
> > //building the route dynamically through refreshed values of bean
> > public String getImapConfig(String imapUrl, String imapPort, String
> pollingInterval, String debugMode) {
> > authenticatorService.updateAuthenticator();
> > return "imaps://" + imapUrl + ":" + imapPort
> > + "?authenticator=#exchangeAuthenticator"
> > + "&debugMode="+ debugMode
> > + "&delete=false"
> > + "&unseen=true"
> > + "&delay=" + pollingInterval
> > + "&mail.imaps.auth.mechanisms=XOAUTH2"
> > + "&disconnect=true"
> > + "&closeFolder=true";
> > }
> >
> > Regards,
> >
> > Dipak.
> >
> >
> >
>