我是 WSO2 ESB 中的新手,我在尝试创建扩展WSO2 SamplingProcessor 类的自定义消息处理器时遇到以下问题。
SamplingProcessor 类扩展了 ScheduledMessageProcessor 抽象类,后者又实现了 MessageProcessor 接口(所以我认为它应该包含所有可以由消息处理器实现的方法。
因此,为了实现我的自定义消息处理器,我使用 pom.xml 文件创建了一个 Maven 项目(它应该符合构建自定义消息处理器的要求) :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.toolkit</groupId>
<artifactId>SamplingProcessorHeaderRateLimitation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>bundle</packaging>
<name>Sampling Processor Header Rate Limitation</name>
<description>Custom Sampling Mesageprocessor using response header to implement the rate limitation</description>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.4</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>com.mycompany.toolkit.messageprocessor</Bundle-SymbolicName>
<Bundle-Name>com.mycompany.toolkit.messageprocessor</Bundle-Name>
<Export-Package>com.mycompany.toolkit.*</Export-Package>
<DynamicImport-Package>*</DynamicImport-Package>
<Implementation-Build>${buildNumber}</Implementation-Build>
<Scm-Root>${project.scm.connection}</Scm-Root>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<releases>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<id>wso2-nexus</id>
<url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<id>wso2-nexus</id>
<url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
</pluginRepository>
</pluginRepositories>
<dependencies>
<dependency>
<groupId>org.apache.synapse</groupId>
<artifactId>synapse-core</artifactId>
<version>2.1.7-wso2v3</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<properties>
<CApp.type>lib/synapse/mediator</CApp.type>
</properties>
</project>
然后我创建了这个SamplingProcessorHeaderRateLimitation,它是我的自定义消息处理器实现,并扩展了SamplingProcessor WSO2消息处理器类:
package com.mycompany.toolkit.messageprocessor;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.SynapseException;
import org.apache.synapse.core.SynapseEnvironment;
import org.apache.synapse.message.processor.impl.ScheduledMessageProcessor;
import org.apache.synapse.message.processor.impl.sampler.SamplingProcessor;
import org.apache.synapse.message.processor.impl.sampler.SamplingProcessorView;
public class SamplingProcessorHeaderRateLimitation extends SamplingProcessor {
private static final Log logger = LogFactory.getLog(ScheduledMessageProcessor.class.getName());
private SamplingProcessorView view;
@Override
public void init(SynapseEnvironment se) {
super.init(se);
logger.info("init() START");
System.out.println("init() START");
try {
view = new SamplingProcessorView(this);
} catch (Exception e) {
throw new SynapseException(e);
}
// register MBean
org.apache.synapse.commons.jmx.MBeanRegistrar.getInstance().registerMBean(view,
"Message Sampling Processor view", getName());
logger.info("init() END");
System.out.println("init() END");
}
@Override
public void setParameters(Map<String, Object> parameters) {
logger.info("setParameters() START");
System.out.println("setParameters() START");
// TODO Auto-generated method stub
super.setParameters(parameters);
logger.info("setParameters() END");
System.out.println("setParameters() END");
}
}
最终我将自定义的 SamplingProcessorHeaderRateLimitation 类用于ESB消息处理器定义(而不是标准的SamplingProcessor):
<?xml version="1.0" encoding="UTF-8"?>
<!---<messageProcessor class="org.apache.synapse.message.processor.impl.sampler.SamplingProcessor" messageStore="transferFromMessageStore" name="transferFromMessageProcessor" xmlns="http://ws.apache.org/ns/synapse">-->
<messageProcessor class="com.mycompany.toolkit.messageprocessor.SamplingProcessorHeaderRateLimitation" messageStore="transferFromMessageStore" name="transferFromMessageProcessor" xmlns="http://ws.apache.org/ns/synapse">
<parameter name="sequence">transferProcessorSequence</parameter>
<parameter name="interval">1000</parameter>
<parameter name="is.active">true</parameter>
<parameter name="concurrency">1</parameter>
</messageProcessor>
此消息处理器为从 transferFromMessageStore 消息存储中检索到的每个元素执行 transferProcessorSequence.xml 序列。
它工作正常,但你可以看到我将 logger.info()放入 init()和 setParameters()方法我的自定义实现。它工作正常,但这些方法只在消息处理器初始化时执行一次。
我需要知道每次从 transferFromMessageStore 消息存储中检索元素时执行的方法是什么,因为这里我必须实现自定义操作。
那么,在从与消息处理器相关的消息存储中检索元素之后以及在执行相关序列之前执行的方法是什么? (在检索元素之后和序列执行之前,我知道方法非常重要)
答案 0 :(得分:1)
为了在每个消息消耗和调度序列之间进行自定义实现,需要扩展类SamplingService。 org.apache.synapse.message.processor.impl.sampler.SamplingService.java
因为它与SamplingService Processor关联org.apache.synapse.task.Task
。
需要覆盖SamplingService中的execute()
或fetch(MessageConsumer msgConsumer)
方法。最好是fetch
方法。另请查看该服务的dispatch()
方法。
public MessageContext fetch(MessageConsumer msgConsumer) {
MessageContext newMsg = super.fetch(msgConsumer);
// custom code here
return newMsg;
}
答案 1 :(得分:0)
使用https://github.com Luke
MessageProcessor code on github
重要的是:boolean setMessageConsumer(MessageConsumer messageConsumer);
List<MessageConsumer> getMessageConsumer();
方法 setMessageConsumer 的文档明确说明。
/** * This method is used to set the message consumer of message processor. Consumer is the one who is * responsible for retrieving messages from a store. * @param messageConsumer is the name of the associated message consumer * @return is true if the message if the message consumer is returned successfully. Otherwise false. */
由于存储和使用来自存储的消息是相反的操作,因此所有这些java类应该彼此相邻。所有你需要的,必须在同一个文件夹周围。