网站建设技术中心,网站做流量是怎么回事,免费建设网站哪个好,wordpress评论审核项在本文中#xff0c;我将展示一个简单的Mule ESB流程#xff0c;以了解实际中使用的Active MQ 的DLQ功能 。 我假设您有一个正在运行的Apache ActiveMQ实例#xff08;如果没有#xff0c;则可以在此处下载一个版本#xff09;。 在此示例中#xff0c;我使用了Mule ESB… 在本文中我将展示一个简单的Mule ESB流程以了解实际中使用的Active MQ 的DLQ功能 。 我假设您有一个正在运行的Apache ActiveMQ实例如果没有则可以在此处下载一个版本。 在此示例中我使用了Mule ESB 3.4.2和ActiveMQ 5.9.0。 我们可以基于以下pom文件创建一个简单的Mule项目 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsdmodelVersion4.0.0/modelVersiongroupIdnet.pascalalma.demo/groupIdartifactIdactivemq-test-flow/artifactIdpackagingmule/packagingname${project.artifactId}/nameversion1.0.0-SNAPSHOT/versionpropertiesmule.version3.4.2/mule.versionproject.build.sourceEncodingUTF-8/project.build.sourceEncodingproject.reporting.outputEncodingUTF-8/project.reporting.outputEncodingjdk.version1.7/jdk.versionjunit.version4.9/junit.versionactivemq.version5.9.0/activemq.version/propertiesdependencies!-- Mule Dependencies --dependencygroupIdorg.mule/groupIdartifactIdmule-core/artifactIdversion${mule.version}/version/dependency!-- Mule Transports --dependencygroupIdorg.mule.transports/groupIdartifactIdmule-transport-jms/artifactIdversion${mule.version}/version/dependencydependencygroupIdorg.mule.transports/groupIdartifactIdmule-transport-vm/artifactIdversion${mule.version}/version/dependency!-- Mule Modules --dependencygroupIdorg.mule.modules/groupIdartifactIdmule-module-client/artifactIdversion${mule.version}/version/dependencydependencygroupIdorg.mule.modules/groupIdartifactIdmule-module-scripting/artifactIdversion${mule.version}/version/dependency!-- for testing --dependencygroupIdorg.mule.tests/groupIdartifactIdmule-tests-functional/artifactIdversion${mule.version}/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion${junit.version}/version/dependencydependencygroupIdorg.apache.activemq/groupIdartifactIdactivemq-client/artifactIdversion${activemq.version}/version/dependency/dependenciesbuildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdversion2.3.2/versionconfigurationsource${jdk.version}/sourcetarget${jdk.version}/targetencoding${project.build.sourceEncoding}/encoding/configuration/pluginplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-resources-plugin/artifactIdversion2.5/versionconfigurationencoding${project.build.sourceEncoding}/encoding/configuration/pluginplugingroupIdorg.mule.tools/groupIdartifactIdmaven-mule-plugin/artifactIdversion1.9/versionextensionstrue/extensionsconfigurationcopyToAppsDirectoryfalse/copyToAppsDirectory/configuration/plugin/plugins/build
/project 这里没有什么特别的。 除了必要的依赖关系之外我还添加了maven-mule-plugin以便可以创建“ mule”打包类型并从IDE运行Mule。 有了此Maven pom我们可以创建以下两个Mule配置。 一个用于测试交易的Mule流 ?xml version1.0 encodingUTF-8?
mule xmlnshttp://www.mulesoft.org/schema/mule/corexmlns:scriptinghttp://www.mulesoft.org/schema/mule/scriptingversionEE-3.4.1xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsdhttp://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsdflow nameMainFlowinbound-endpoint refevent-queue /logger categorynet.pascalalma.demo.MainFlow levelINFO messageReceived message from activeMQ /scripting:componentscripting:script engineGroovythrow new Exception(Soap Fault Response detected)/scripting:script/scripting:componentoutbound-endpoint refresult-queue //flow
/mule 在此流程中我们从入站端点接收到一条消息记录一条消息并引发异常然后将该消息放入下一个队列。 如我们所见我没有添加任何异常处理程序。 端点和连接器的配置如下所示 ?xml version1.0 encodingUTF-8?mule xmlnshttp://www.mulesoft.org/schema/mule/corexmlns:jmshttp://www.mulesoft.org/schema/mule/jmsxmlns:springhttp://www.springframework.org/schema/beansversionEE-3.4.1xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsdhttp://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsdspring:bean idredeliveryPolicy classorg.apache.activemq.RedeliveryPolicyspring:property namemaximumRedeliveries value5/spring:property nameinitialRedeliveryDelay value500/spring:property namemaximumRedeliveryDelay value10000/spring:property nameuseExponentialBackOff valuefalse/spring:property namebackOffMultiplier value3//spring:bean!-- ActiveMQ Connection factory --spring:bean idamqFactory classorg.apache.activemq.ActiveMQConnectionFactory lazy-inittruespring:property namebrokerURL valuetcp://localhost:61616 /spring:property nameredeliveryPolicy refredeliveryPolicy //spring:beanjms:activemq-connector nameactiveMqConnectorconnectionFactory-refamqFactorypersistentDeliverytruenumberOfConcurrentTransactedReceivers2specification1.1 /jms:endpoint nameevent-queue connector-refactiveMqConnector queueevent-queue jms:transaction actionALWAYS_BEGIN //jms:endpointjms:endpoint nameresult-queue connector-refactiveMqConnector queueresult-queue jms:transaction actionALWAYS_JOIN //jms:endpoint
/mule 我为ActiveMQ连接工厂定义了一个Spring bean并为该工厂的重新交付策略定义了一个Spring bean。 使用此重新分发策略我们可以配置当原始尝试失败时Mule应该重试处理队列中消息的频率。 重新交付策略中的一个不错的功能是“ backOffMultiplier”和“ useExponentialBackOff”组合。 使用这些选项您可以使两次重新交付尝试之间的间隔成倍增加直到达到maximumRedeliveryDelay。 在那种情况下M子将等待“ maximumRedeliveryDelay”等待下一次尝试。 因此使用这些配置我们可以创建一个Mule测试类并运行它。 测试类如下所示 package net.pascalalma.demo;import org.junit.Test;
import org.mule.DefaultMuleMessage;
import org.mule.api.MuleMessage;
import org.mule.module.client.MuleClient;
import org.mule.tck.junit4.FunctionalTestCase;public class TransactionFlowTest extends FunctionalTestCase {Overrideprotected String getConfigResources() {return app/test-flow.xml, app/test-endpoints.xml;}Testpublic void testError() throws Exception {MuleClient client new MuleClient(muleContext);MuleMessage inMsg new DefaultMuleMessage(txtSome message/txt, muleContext);client.dispatch(event-queue, inMsg);// Give Mule the chance to redeliver the messageThread.sleep(4000);}
} 如果我们运行此测试您将在日志中看到如下消息 Exception stack is:
1. Message with id ID:Pascals-MacBook-Pro-2.local-59158-1406440948059-1:1:3:1:1 has been redelivered 3 times on endpoint jms://event-queue, which exceeds the maxRedelivery setting of 0 on the connector activeMqConnector. Message payload is of type: ActiveMQTextMessage (org.mule.transport.jms.redelivery.MessageRedeliveredException)org.mule.transport.jms.redelivery.JmsXRedeliveryHandler:87 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/transport/jms/redelivery/MessageRedeliveredException.html) 如果现在我们切换到ActiveMQ控制台 可以通过http// localhost8161进行默认本地安装则可以看到以下队列 不出所料我们看到正在创建两个队列事件队列为空默认的ActiveMQ.DLQ包含我们的消息 正如您可以想象的那样为每个队列使用一个特定的DLQ而不是一个包含各种无法传递的消息的DLQ可能很方便。 幸运的是这很容易在ActiveMQ中配置。 只需将以下内容放在“ $ ACTIVEMQ_HOME / conf”文件夹中的“ activemq.xml”文件中。 !-- Set the following policy on all queues using the wildcard --
policyEntry queuedeadLetterStrategyindividualDeadLetterStrategy queuePrefixDLQ. useQueueForQueueMessagestrue //deadLetterStrategy
/policyEntry 如果现在重新启动ActiveMQ请删除现有队列并重新运行测试我们将看到以下结果 因此使用此设置每个队列都有自己的DLQ。 有关这些ActieMQ设置的更多选项请参见此处 。 通过本文中创建的Mule流可以轻松测试和使用这些设置。 翻译自: https://www.javacodegeeks.com/2014/07/mule-esb-activemq-and-the-dlq.html