How to access SwitchYard Context properties from a @Named CDI bean in SwitchYard Camel route

Solution Verified - Updated -

Issue

  • I would like to access SwitchYard Context properties in a @Named CDI bean for getting CamelFileName from the SFTP service binding.
  • I tried Injecting the Context object into the named bean as follows:
import org.apache.camel.Message;

@Named
public class MyAction {

    @Inject  
    private Context context;

    public void displayMessage(Message message) {
        System.out.println("File name is " + context.getProperty("CamelFileName").getValue());
    }
  • However, I got the following error:
Caused by: java.lang.IllegalStateException: SWITCHYARD030422: Illegal call to get the SwitchYard Context; must be called within the execution of an ExchangeHandler chain.
    at org.switchyard.component.bean.internal.context.ContextProxy.getContext(ContextProxy.java:156) [switchyard-component-bean-1.1.1-p5-redhat-1.jar:1.1.1-p5-redhat-1]
    at org.switchyard.component.bean.internal.context.ContextProxy.getProperty(ContextProxy.java:42) [switchyard-component-bean-1.1.1-p5-redhat-1.jar:1.1.1-p5-redhat-1]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_07]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [rt.jar:1.6.0_07]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [rt.jar:1.6.0_07]
    at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_07]
    at org.jboss.weld.bean.proxy.AbstractBeanInstance.invoke(AbstractBeanInstance.java:45) [weld-core-1.1.13.Final-redhat-1.jar:1.1.13.Final-redhat-1]
    at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:105) [weld-core-1.1.13.Final-redhat-1.jar:1.1.13.Final-redhat-1]
    at org.jboss.weld.proxies.Context$1366014918$Proxy$_$$_WeldClientProxy.getProperty(Context$1366014918$Proxy$_$$_WeldClientProxy.java) [weld-core-1.1.13.Final-redhat-1.jar:]
    at com.example.myapp.actions.MyAction.displayMessage(MyAction.java:21) [my-application.jar:]
  • How can I access Context properties from a named CDI bean in SwitchYard?

Environment

  • Red Hat JBoss Fuse Service Works (FSW)
    • 6.x

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.