0
votes

Getting org.mule.api.expression.ExpressionRuntimeException on running below snippet in Mule

I have stored value in var1 and var2 as [message.inboundProperties.'http.query.params'.value1] and [message.inboundProperties.'http.query.params'.value2] respectively.

I'm trying to return sum of the parameters passed.

import java.lang.Integer;

int firstValue = Integer.pareseint(flowVars.var1);
int secondValue =Integer.pareseint(flowVars.var2);
int result =  firstValue + secondValue;
payload =  result

UPDATE: I've resolved the error but now it is concatenating the inputs not adding them. Other operators like *, /, -, etc work fine.

3

3 Answers

0
votes

You can try using Mule Expression as: #[2 + 4]

0
votes

You can try it in the following way using java transformer component. It's working for me.

Below is the main mule xml config file

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="tryoutFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/bill" allowedMethods="GET" doc:name="HTTP"/>
        <set-variable variableName="var1" value="#[message.inboundProperties.'http.query.params'.value1]" doc:name="var1"/>
        <set-variable variableName="var2" value="#[message.inboundProperties.'http.query.params'.value2]" doc:name="var2"/>
        <custom-transformer class="org.mule.transformar.Addition" returnClass="java.lang.Integer doc:name="Java">
        </custom-transformer>
    </flow>
</mule>

And below are the java class which java transformer component use to transform your message. Remember put the java class under src/main/java folder.

org.mule.transformar.Addition

package org.mule.transformar;

import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.api.transport.PropertyScope;
import org.mule.transformer.AbstractMessageTransformer;

public class Addition extends AbstractMessageTransformer{

    @Override
    public Integer transformMessage(MuleMessage message, String outputEncoding) throws TransformerException {
        // TODO Auto-generated method stub

        Integer i1 = Integer.parseInt(message.getProperty("var1", PropertyScope.INVOCATION));

        Integer i2 = Integer.parseInt(message.getProperty("var2", PropertyScope.INVOCATION));

        Integer returnVal =  i1 + i2;

        return returnVal;
    }
}
0
votes

You have done everything right, but few Syntax error like Integer.pareseint instead of Integer.parseInt ...

Please refer the following flow to make it work in a easy way:-

 <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="testFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
        <set-variable doc:name="Variable" value="#[message.inboundProperties.'http.query.params'.value1]" variableName="var1"/>
        <set-variable doc:name="Variable" value="#[message.inboundProperties.'http.query.params'.value2]" variableName="var2"/>
        <expression-component doc:name="Expression"><![CDATA[
import java.lang.Integer;

int firstValue = Integer.parseInt(flowVars.var1);
int secondValue = Integer.parseInt(flowVars.var2);
int result =  firstValue + secondValue;
payload =  result]]></expression-component>
        <object-to-string-transformer doc:name="Object to String"/>
        <logger message="#['Total '+message.payload]" level="INFO" doc:name="Logger"/>
    </flow>

The url to test is :- http://localhost:8081/test/?value1=12&value2=100