Red Hat Training

A Red Hat training course is available for Red Hat Fuse

57.4. Camel コンテキスト設定

デフォルトの CamelContext Bean の名前を変更するだけの場合は、Camel CDI によって提供される @ContextName 修飾子を使用できます。

@ContextName("camel-context")
class MyRouteBean extends RouteBuilder {
 
    @Override
    public void configure() {
        from("jms:invoices").to("file:/invoices");
    }
}

それ以外の場合、さらにカスタマイズが必要な場合は、任意の CamelContext クラスを使用してカスタム Camel コンテキスト Bean を宣言できます。次に、@PostConstruct および @PreDestroy ライフサイクルコールバックを実行して、カスタマイズを行うことができます。

@ApplicationScoped
class CustomCamelContext extends DefaultCamelContext {

    @PostConstruct
    void customize() {
        // Set the Camel context name
        setName("custom");
        // Disable JMX
        disableJMX();
    }

    @PreDestroy
    void cleanUp() {
        // ...
    }
}

Producer メソッドdisposer メソッドを使用して、Camel コンテキスト Bean をカスタマイズすることもできます。次に例を示します。

class CamelContextFactory {

    @Produces
    @ApplicationScoped
    CamelContext customize() {
        DefaultCamelContext context = new DefaultCamelContext();
        context.setName("custom");
        return context;
    }

    void cleanUp(@Disposes CamelContext context) {
        // ...
    }
}

同様に、producer fields を使用できます。

@Produces
@ApplicationScoped
CamelContext context = new CustomCamelContext();

class CustomCamelContext extends DefaultCamelContext {

    CustomCamelContext() {
        setName("custom");
    }
}

このパターンは、たとえば、コンテナーが setAutoStartup メソッドを呼び出して初期化するときに Camel コンテキストルートが自動的に開始されるのを回避するために使用できます。

@ApplicationScoped
class ManualStartupCamelContext extends DefaultCamelContext {

    @PostConstruct
    void manual() {
        setAutoStartup(false);
    }
}