Hi, I'll rephrase my question to put it more succinctly. As specified in this link* https://camel.apache.org/components/4.10.x/others/mail-microsoft-oauth.html <https://camel.apache.org/components/4.10.x/others/mail-microsoft-oauth.html>* I want my * exchangeAuthenticator bean to be dynamically updated. * *How do I do that? lookup-unbind and bind does not seem to work.*
try { tenantId, clientId, clientSecret, wsAdapterMailBox); MicrosoftExchangeOnlineOAuth2MailAuthenticator newExchangeAuthenticator = new MicrosoftExchangeOnlineOAuth2MailAuthenticator( tenantId, clientId, clientSecret, wsAdapterMailBox); newExchangeAuthenticator.toString()); 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); MicrosoftExchangeOnlineOAuth2MailAuthenticator newAuthenticatorBean = registry.lookupByNameAndType("exchangeAuthenticator", MicrosoftExchangeOnlineOAuth2MailAuthenticator.class); } catch (Exception e) { } On Wed, Mar 5, 2025 at 11:40 AM Dipak Rai <fullstackdipak...@gmail.com> wrote: > Hi Riccardo, > > Thank you so much for your answers on my thread request. > I have already kept my Route building logic in a separate class. And only > to build the IMAP *from *route dynamically I have a separate method > *getImapConfig > .* > > After putting some log statements and checking the hashcode of the > 'exchangeAuthenticator' I see that the '*exchangeAuthenticator*' bean is > getting bound and unbound to the registry successfully. But the > 'exchangeAutheticator' in the String being returned somehow refers to the > old values of exchangeAuthenticator. Is there a better way to build this > IMAP connection string so that the cached values of the bean are not > referred to? > > > public String getImapConfig(String imapUrl, String imapPort, String > pollingInterval, String debugMode) { > log.info("Inside getImapConfig imapUrl {} imapPort {} PollingInterval {} > debugMode {} ", imapUrl, imapPort, > pollingInterval, debugMode); > authenticatorService.updateAuthenticator(); > MicrosoftExchangeOnlineOAuth2MailAuthenticator exchangeAuthenticator = > camelContext.getRegistry() > .lookupByNameAndType("exchangeAuthenticator", > MicrosoftExchangeOnlineOAuth2MailAuthenticator.class); > if (null!=exchangeAuthenticator) { > log.info(" @@## hashcode for authenticator Bean inside getImapConfig > {}",exchangeAuthenticator.hashCode()); > } > return "imaps://" + imapUrl + ":" + imapPort > + "?authenticator=#exchangeAuthenticator" > + "&debugMode="+ debugMode > + "&delete=false" > + "&unseen=true" > + "&delay=" + pollingInterval > + "&mail.imaps.auth.mechanisms=XOAUTH2" > + "&disconnect=true" > + "&closeFolder=true"; > } > > -Dipak. > > On Tue, Mar 4, 2025 at 10:28 PM Modanese, Riccardo > <riccardo.modan...@eurotech.com.invalid> wrote: > >> Hi, >> >> you should proceed splitting the implementation in 2 components. >> >> >> >> Implement your own org.apache.camel.builder.RouteBuilder to create routes >> >> Implement your own >> org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor >> to inject beans in the context >> >> >> >> Without Spring the solution I adopted was to create the Camel context >> with my Registry instance >> >> >> >> org.apache.camel.support.DefaultRegistry defaultRegistry = new >> DefaultRegistry(); >> >> //register your beans >> >> org.apache.camel.CamelContext camelContext = new >> DefaultCamelContext(defaultRegistry); >> >> >> >> This is not dynamic, so I suppose you have to intercept some event. >> >> >> >> Hope this can help. >> >> >> From: Dipak Rai <fullstackdipak...@gmail.com> >> Date: Tuesday, 4 March 2025 at 17:38 >> To: users@camel.apache.org <users@camel.apache.org> >> Subject: Re: Refreshing Bean To Build An IMAP Route Should Have Worked >> The registry.bind () documentation says that "*Binds the bean to the >> repository (if possible). If the bean is CamelContextAware then the >> registry will automatic inject the context if possible."* >> >> What does that mean ? How can I make the 'exchangeAuthenticator' bean >> CamelContextAware? >> >> On Tue, Mar 4, 2025 at 9:48 PM Dipak Rai <fullstackdipak...@gmail.com> >> wrote: >> >> > Hi, >> > >> > To add to my code snippet, I see in the logs that I could unbind the >> bean >> > and rebind it but somehow in the route url this string >> *"?authenticator=#exchangeAuthenticator" >> > *does not show the updated values. I still get the error for the wrong >> *tenantID >> > *I passed deliberately before last tim >> > *e.* >> > >> > *ERROR:* >> > >> > *c.n.w.r.RouterUtil: Exception occurred while creating IMAP route: >> > {}Failed to start route mail-route-imap because of >> > com.microsoft.aad.msal4j.MsalServiceException: AADSTS900023: Specified >> > tenant identifier '5d471751-******-70f44f9630b' is neither a valid DNS >> > name, nor a valid external domain. * >> > >> > *Old wrong tenantID: * *5d471751-******-70f44f9630b* >> > New correct tenantID passed to the bean: >> *5d471751-******-70f44f9630b**0* >> > *But this last correct one is not picked up by the route.* >> > >> > Regards, >> > Dipak. >> > >> > >> > On Tue, Mar 4, 2025 at 8:53 PM Dipak Rai <fullstackdipak...@gmail.com> >> > wrote: >> > >> >> 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 >> >> <riccardo.modan...@eurotech.com.invalid> 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 <fullstackdipak...@gmail.com> >> >>> Date: Tuesday, 4 March 2025 at 11:47 >> >>> To: users@camel.apache.org <users@camel.apache.org> >> >>> 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 < >> fullstackdipak...@gmail.com> >> >>> 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. >> >>> > >> >>> > >> >>> > >> >>> >> >> >> >