我想通过使用Java DSL,嵌入式ActiveMQ和KahaDB作为持久性适配器来实现基本的deadLetterQueue。我的C:/JavaProjects/tmp/input
目录中有一个文本文件。我想将文件放入内部队列,通过调用.rollback()产生不成功的传递,然后将消息转发给deadLetterQueue,之后,我将从DeadLetterQueue中获取数据并将其放入C:/JavaProjects/tmp/output
目录中。所有消息均已成功传递到incoming
,但显然它们不会进入死信队列。我怎么做?
这是我的主要课程:
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter;
import org.apache.activemq.store.kahadb.plist.PListStoreImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import java.io.File;
import java.util.Arrays;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Value("${activemq.broker-url}")
private String brokerUrl;
@Value("${activemq.user}")
private String userName;
@Value("${activemq.password}")
private String password;
@Bean
public BrokerService setupBroker() {
try {
brokerService.addConnector(brokerUrl);
brokerService.setUseJmx(true);
brokerService.setUseShutdownHook(false);
brokerService.setAdvisorySupport(true);
brokerService.setEnableStatistics(true);
brokerService.setPersistenceAdapter(createKahaDBPersistenceAdapter());
brokerService.setTempDataStore(createKahaDBTempDataStore());
brokerService.start();
Thread.sleep(6000);
brokerService.stop();
} catch (Exception e) {
e.printStackTrace();
}
return brokerService;
}
private KahaDBPersistenceAdapter createKahaDBPersistenceAdapter() {
final KahaDBPersistenceAdapter kahaDBPersistenceAdapter = new KahaDBPersistenceAdapter();
kahaDBPersistenceAdapter.setDirectory(new File("C:\\JavaProjects\\tmp", "activemq/kahadb"));
kahaDBPersistenceAdapter.setCompactAcksIgnoresStoreGrowth(true);
kahaDBPersistenceAdapter.setCompactAcksAfterNoGC(5);
return kahaDBPersistenceAdapter;
}
private PListStoreImpl createKahaDBTempDataStore() {
final PListStoreImpl tempKahaDBStore = new PListStoreImpl();
tempKahaDBStore.setDirectory(new File("C:\\JavaProjects\\tmp", "activemq/tmp"));
return tempKahaDBStore;
}
@Bean
public ActiveMQConnectionFactory activeMQConnectionFactory() {
final ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost");
factory.setTrustedPackages(Arrays.asList("com.example.demo"));
return factory;
}
}
这是我的路由器:
@Component
public class MyRouter extends RouteBuilder {
private static Logger logger = LoggerFactory.getLogger(MyRouter.class);
@Override
public void configure() throws Exception {
onException(Exception.class)
.log(LoggingLevel.ERROR, logger, "THE ERROR IS: " + exceptionMessage().toString());
errorHandler(deadLetterChannel("activemq:queue:deadLetterQueue")
.deadLetterHandleNewException(true)//guarantees to deadLetterChannel to complete.
.useOriginalMessage()
.backOffMultiplier(2)
.redeliveryDelay(100)
.maximumRedeliveries(3)
.log("TO DEAD LETTER QUEUE!"));
/*
* Receiving data to an internal queue "incoming",
* then calling rollback() to send the info from internal queue to dead Letter queue
* */
from("file:/JavaProjects/tmp/input?noop=true")
.to("activemq:queue:incoming");
from("activemq:queue:incoming")
.process(new MyProcessor())
.rollback("'INSIDE ROLLBACK' Lets go to the DEAD QUEUE!")
.to("file:/JavaProjects/tmp/output");
//Getting data from Dead Letter Queue and put it to our output folder
from("activemq:queue:deadLetterQueue")
.process(new MyProcessor())
.to("file:/JavaProjects/tmp/output");
}
}
这是我的处理器:
public class MyProcessor implements Processor{
@Override
public void process(Exchange exchange) throws Exception {
System.out.println("\nMessage ID is: " + exchange.getIn().getMessageId());
System.out.println("\nFile`s content is: " + exchange.getIn().getBody(String.class) + "\n");
}
}
任何帮助将不胜感激。