如何在Mule流程启动时读取属性文件

时间:2014-10-30 19:06:46

标签: mule mule-studio mule-component

我正在创建一个主要包含2个节点的流: 1)WMB节点 - 它从MQ中选择消息 2)Java节点 - 它转换和处理消息。

我创建了一个属性文件,其中包含一些用于实现业务逻辑的值。在执行流程期间,我的java类读取该属性文件。

因此,根据当前的实现,只要MQ中有新消息,java类就会加载属性文件。因此,通过这种方式,如果队列中有'n'个消息,则流读取属性文件'n'次。

但是我想要在我们部署/重新启动流而不是每次执行流时,只读取一次属性文件。

仅供参考,我在项目中没有使用spring框架。

1 个答案:

答案 0 :(得分:0)

哟可以在你的Mule流程中声明以下内容,并且你不需要Spring bean: -

 <context:property-placeholder location="classpath:yourpropertFileName.properties"/>

服务器启动/重新启动后将只读取一次

<强>更新

假设您有一个名为yourpropertFileName.properties的属性文件,并且您已在其中定义了以下键和值: -

message1=This is message1 value
message2=This is message2 value

现在您可以在Mule Flow中使用它,如下所示: -

<logger message="${message1}" level="INFO" />
<logger message="${message2}" level="INFO" />

正如您所看到的,我已经从属性文件中读取了键和值,并在Mule Config文件的记录器中使用它。您可以在任何您想要的Mule组件中使用属性文件中的键

<强>更新: - 下面是一个示例Java类,它从属性文件中读取值。您可以根据您的要求进行修改: -

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

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

    public class SampleJavaClass extends AbstractMessageTransformer {

        Properties prop = new Properties(); //Creating property file object read File attachment path from property file
        InputStream input = null; // To read property file path

        @Override
        public Object transformMessage(MuleMessage message, String outputEncoding)
                throws TransformerException {

             try {
                input = getClass().getResourceAsStream("yourpropertFileName.properties"); // Property file path in classpath
                 prop.load(input); // get and load the property file
                String msg1=prop.getProperty("message1");
                String msg2=prop.getProperty("message2");
                System.out.println("Key1 from Prop file "+msg1);
                System.out.println("Key2 from Prop file "+msg2);
            } catch (IOException e)
            {

                e.printStackTrace();
            }

              return message;
            }
        }