You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何CamelRegistry无法更新MicrosoftExchangeOnlineOAuth2MailAuthenticator Bean?

Spring Boot + Camel动态更新IMAP ExchangeAuthenticator Bean失败问题

我正在开发基于Spring Boot + Camel的应用,通过Java DSL动态构建IMAP连接。当前遇到的问题是:尽管能动态读取所有IMAP参数,但无法重新注册带有更新值的exchangeAuthenticator Bean,Camel始终引用之前(测试时故意配置错误的)旧值,导致IMAP路由重建失败,异常提示已提供正确的客户端密钥,但Bean仍指向旧值。

临时解决方案是修正OAuth2凭证后必须重启应用,exchangeAuthenticator Bean才能刷新为最新值,但重启应用并非可行方案。我使用的是Spring 3和Camel 4.7.0。

已尝试的方案

  • 将ExchangeAuthenticator注入为prototype类型Bean,无效;
  • 从Camel注册表中解绑该Bean,同样无效。

代码片段

AuthenticatorService.java(负责重新注册Bean)

@Service
public class AuthenticatorService {
    @Autowired
    private CamelContext camelContext;
    @Autowired
    private EmailConfigBean emailConfigBean;
    public void updateAuthenticator() {
        String tenantId = emailConfigBean.getTenantId();
        String clientId = emailConfigBean.getClientId();
        String clientSecret = emailConfigBean.getClientSecret();
        String myMailBox = emailConfigBean.getmyMailBox();

        try {
            // 日志确认已正确获取最新值
            MicrosoftExchangeOnlineOAuth2MailAuthenticator authenticator = new MicrosoftExchangeOnlineOAuth2MailAuthenticator(
                    tenantId, clientId, clientSecret, myMailBox);
            Registry registry = camelContext.getRegistry();
            registry.unbind("exchangeAuthenticator");
            registry.bind("exchangeAuthenticator", authenticator);
        } catch (Exception e) {
            // 处理异常
        }
    }
    @Bean
    public CamelContextConfiguration contextConfiguration() {
        return new CamelContextConfiguration() {
            @Override
            public void beforeApplicationStart(CamelContext context) {
                updateAuthenticator();
            }
            @Override
            public void afterApplicationStart(CamelContext context) {
                // 启动后无需操作
            }
        };
    }
}

RouteUtil.java(处理路由逻辑)

private void reCreateImapRoute(String imapMailId, String imapPort, String pollingInterval, String debugMode) {
        try {
            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");
                    }
                });
        } catch (MsalServiceException e) {
            // 处理异常
        } catch (Exception e) {
            // 处理异常
        }
}

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";
}

内容的提问来源于stack exchange,提问作者raikumardipak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 01:37:13