Camel Components

MVEL

Since Camel 2.12

Only producer is supported

The MVEL component allows you to process a message using an MVEL template. This can be ideal when using templating to generate responses for requests.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-mvel</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

mvel:templateName[?options]

Where templateName is the classpath-local URI of the template to invoke; or the complete URL of the remote template (e.g.: file://folder/myfile.mvel).

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.

For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.

Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.

You can configure components using:

  • the Component DSL.

  • in a configuration file (application.properties, *.yaml files, etc).

  • directly in the Java code.

Configuring Endpoint Options

You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.

Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.

A good practice when configuring options is to use Property Placeholders.

Property placeholders provide a few benefits:

  • They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.

  • They allow externalizing the configuration from the code.

  • They help the code to become more flexible and reusable.

The following two sections list all the options, firstly for the component followed by the endpoint.

Component Options

The MVEL component supports the following options which are listed below.

Name Description Default Type

allowContextMapAll (producer)

Whether the variables that the producer gives the MVEL template also include exchange, in, request, out, response, exchangeProperty, exchangeProperties and camelContext (out and response only when the exchange pattern is InOut). If false, the template gets only body, header, headers, variable, variables and exception. Enabling it gives the template the whole Exchange and CamelContext API. It does not limit MVEL itself, which can call any Java class in either case. The default for the endpoints of the component.

false

boolean

allowTemplateFromHeader (producer)

Whether the producer reads the CamelMvelResourceUri and CamelMvelTemplate headers. If false, the producer ignores both headers. A template from a header can do all that MVEL allows, such as calling the methods of any Java class, for example System or Runtime, and the CamelMvelResourceUri header can name any resource, such as a file or an http URL. Enable it only when the route sets these headers, not a message sender. The default for the endpoints of the component.

false

boolean

contentCache (producer)

Whether the endpoints of the component cache their template, unless the contentCache option of the endpoint says otherwise. If true, the producer reads the template of resourceUri once and keeps its content, so a change to the resource is only seen after the content cache is cleared, for example with the clearContentCache JMX operation. If false, the producer reads the resource on each exchange, and compiles it again only when its text changed.

true

boolean

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

autowiredEnabled (advanced)

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

Endpoint Options

The MVEL endpoint is configured using URI syntax:

mvel:resourceUri

With the following path and query parameters:

Path Parameters

Name Description Default Type

resourceUri (producer)

Required Path to the resource. You can prefix with: classpath, file, http, ref, or bean. classpath, file and http loads the resource using these protocols (classpath is default). ref will lookup the resource in the registry. bean will call a method on a bean to be used as the resource. For bean you can specify the method name after dot, eg bean:myBean.myMethod.

String

Query Parameters

Name Description Default Type

allowContextMapAll (producer)

Sets whether the context map should allow access to all details. By default only the message body and headers can be accessed. This option can be enabled for full access to the current Exchange and CamelContext. Doing so impose a potential security risk as this opens access to the full power of CamelContext API.

false

boolean

allowTemplateFromHeader (producer)

Whether the producer reads the CamelMvelResourceUri and CamelMvelTemplate headers. If false, the producer ignores both headers. A template from a header can do all that MVEL allows, such as calling the methods of any Java class, for example System or Runtime, and the CamelMvelResourceUri header can name any resource, such as a file or an http URL. Enable it only when the route sets these headers, not a message sender.

false

boolean

contentCache (producer)

Sets whether to use resource content cache or not.

true

boolean

encoding (producer)

The name of the character encoding (charset) that the producer uses to read the template of resourceUri, for example UTF-8. If not set, the default charset of the JVM is used. Not used for the template of the CamelMvelTemplate header, which is already text.

String

lazyStartProducer (producer (advanced))

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

Message Headers

The MVEL component supports the following message header(s), which is/are listed below:

Name Description Default Type

CamelMvelResourceUri (producer)

Constant: MVEL_RESOURCE_URI

The URI of a template that the producer renders in place of the template of resourceUri, in the same form as resourceUri. Only used when allowTemplateFromHeader is true. The producer removes the header and hands the exchange to the mvel endpoint of that URI, with the same options as this endpoint, which renders the text of a CamelMvelTemplate header instead if one is set.

String

CamelMvelTemplate (producer)

Constant: MVEL_TEMPLATE

The text of an MVEL template that the producer renders in place of the template of resourceUri. Only used when allowTemplateFromHeader is true. The producer compiles it on each exchange, as it is not cached, and removes the header after reading it.

String

Message Body

The message body at each side of the MVEL component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.

Side Body Description

Producer input

any

The template gets the body as body, as it is: no body prints as null, and bytes or a stream are not read as text. The template is the resource, or the CamelMvelTemplate header with allowTemplateFromHeader=true, never the body.

Producer output

text (java.lang.String)

The text the template writes. The headers are kept.

The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.

Usage

MVEL Context

Camel will provide exchange information in the MVEL context (just a Map). The Exchange is transferred as: The body, header, headers, variable, variables and exception entries are always there; the others only when the endpoint has allowContextMapAll=true.

key value

exchange

The Exchange itself

exchangeProperty

The Exchange properties

exchangeProperties

The Exchange properties

variable

The variables

variables

The variables

header

The headers of the message

headers

The headers of the message

exception

The exception of the exchange, if any

camelContext

The CamelContext

request

The message

in

The message

body

The message body

out

The Out message (only for InOut message exchange pattern).

response

The Out message (only for InOut message exchange pattern).

Hot reloading

By default (contentCache=true), Camel loads the mvel template resource only once, and thus hot reloading is not possible. If you set contentCache=false, Camel reads the resource on each message, so changes to file and classpath resources (expanded jar) are picked up, and compiles the template again when its text changed.

Dynamic templates

Camel provides two headers by which you can define a different resource location for a template, or the template content itself. If any of these headers is set, and the endpoint has allowTemplateFromHeader=true, then Camel uses this over the endpoint configured resource. This allows you to provide a dynamic template at runtime.

Examples

For example, you could use something like

  • Java

  • XML

  • YAML

from("activemq:My.Queue")
    .to("mvel:com/acme/MyResponse.mvel");
<route>
  <from uri="activemq:My.Queue"/>
  <to uri="mvel:com/acme/MyResponse.mvel"/>
</route>
- route:
    from:
      uri: activemq:My.Queue
      steps:
        - to:
            uri: mvel:com/acme/MyResponse.mvel

To use a MVEL template to formulate a response to a message for InOut message exchanges (where there is a JMSReplyTo header).

To specify what template the component should use dynamically via a header, so for example:

  • Java

  • XML

  • YAML

from("direct:in")
    .setHeader("CamelMvelResourceUri").constant("path/to/my/template.mvel")
    .to("mvel:dummy?allowTemplateFromHeader=true");
<route>
  <from uri="direct:in"/>
  <setHeader name="CamelMvelResourceUri">
    <constant>path/to/my/template.mvel</constant>
  </setHeader>
  <to uri="mvel:dummy?allowTemplateFromHeader=true"/>
</route>
- route:
    from:
      uri: direct:in
      steps:
        - setHeader:
            name: CamelMvelResourceUri
            expression:
              constant:
                expression: path/to/my/template.mvel
        - to:
            uri: mvel:dummy
            parameters:
              allowTemplateFromHeader: true

To specify a template directly as a header, the component should use dynamically via a header, so for example:

  • Java

  • XML

  • YAML

from("direct:in")
    .setHeader("CamelMvelTemplate").constant("@{\"The result is \" + body * 3}")
    .to("mvel:dummy?allowTemplateFromHeader=true");
<route>
  <from uri="direct:in"/>
  <setHeader name="CamelMvelTemplate">
    <constant>@{"The result is " + body * 3}</constant>
  </setHeader>
  <to uri="mvel:dummy?allowTemplateFromHeader=true"/>
</route>
- route:
    from:
      uri: direct:in
      steps:
        - setHeader:
            name: CamelMvelTemplate
            expression:
              constant:
                expression: "@{\"The result is \" + body * 3}"
        - to:
            uri: mvel:dummy
            parameters:
              allowTemplateFromHeader: true