Spring integration 如何在spring集成中在运行时解析jms出站目标队列,
因此,在这里,目标队列由源目录中的.txt文件中的MSG_类型确定,该文件是集成流的源 如果MSG_类型以“ORU”开始发送到队列“jms/dataqueue”,如果MSG_类型是“MHS”发送到“jms/headersQueue”之类的东西。我在这里使用wls JmsTemplate。那么,如何根据从文本文件中获取的Msgtype解析目的地呢。 请参阅方法sendToJmsQueue,我希望在运行时确定inboundDataQueue 请参阅下面的源代码Spring integration 如何在spring集成中在运行时解析jms出站目标队列,,spring-integration,spring-jms,spring-integration-dsl,Spring Integration,Spring Jms,Spring Integration Dsl,因此,在这里,目标队列由源目录中的.txt文件中的MSG_类型确定,该文件是集成流的源 如果MSG_类型以“ORU”开始发送到队列“jms/dataqueue”,如果MSG_类型是“MHS”发送到“jms/headersQueue”之类的东西。我在这里使用wls JmsTemplate。那么,如何根据从文本文件中获取的Msgtype解析目的地呢。 请参阅方法sendToJmsQueue,我希望在运行时确定inboundDataQueue 请参阅下面的源代码 @EnableJms @Configu
@EnableJms
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class IntegrationConfig {
private final Logger LOGGER = LoggerFactory.getLogger(IntegrationConfig.class);
private QueueConnectionFactory factory;
private InitialContext jndi;
//private QueueSession session = null;
@Value("${spring.wls.InboundDataQueue}")
private String inboundDataQueue;
@Value("${spring.wls.InboundSolicitedQueue}")
private String inboundSolicitedQueue;
@Value("${spring.wls.InboundQueryQueue}")
private String inboundQueryQueue;
@Value("${spring.wls.InboundAckQueue}")
private String inboundAckQueue;
@Autowired
public FileProcessor fileProcessor;
@Autowired
public OutboundGatewayConfig outboundGatewayConfig;
@Bean
public MessageChannel fileInputChannel() {
return new DirectChannel();
}
@Bean
public MessageChannel jmsOutboundChannel() {
return new DirectChannel();
}
@Bean
public MessageChannel customErrorChannel() {
return new DirectChannel();
}
@Bean
public FileToStringTransformer fileToStringTransformer() {
return new FileToStringTransformer();
}
@Bean
public JmsTemplate getJmsTemplate(@Value("${spring.wls.jndiContext}") String context,
@Value("${spring.wls.InboundServerURL}") String serverName,
@Value("${spring.wls.inboundConnfactory}") String factoryName) {
if (jndi == null) {
// Create a context
Hashtable data = new Hashtable();
data.put(Context.INITIAL_CONTEXT_FACTORY, context);
data.put(Context.PROVIDER_URL, serverName);
try {
jndi = new InitialContext(data);
} catch (NamingException e) {
LOGGER.error("Failed to initialize JNDI context:: ", e);
}
}
if (factory == null) {
// Look up JMS connection factory
/*
* factory = (QueueConnectionFactory)PortableRemoteObject.narrow( jndi.lookup(
* factoryName ), QueueConnectionFactory.class );
*/
try {
factory = (QueueConnectionFactory) (jndi.lookup(factoryName));
} catch (NamingException e) {
LOGGER.error("Error in Creating Weblogic JNDI Connection factory:: ", e);
//throw new BeanCreationException("JmsTemplate", "Failed to create a JmsTemplate", e);
}
}
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(factory);
JmsTemplate wlsJmsTemplate = new JmsTemplate();
wlsJmsTemplate.setConnectionFactory(cachingConnectionFactory);
return wlsJmsTemplate;
}
@Bean
public IntegrationFlow processFile() {
return IntegrationFlows
.from("fileInputChannel")
.transform(fileToStringTransformer())
.handle("fileProcessor", "process")
.log(LoggingHandler.Level.INFO, "process file", m -> m.getHeaders().get("Message_Type"))
.channel(this.jmsOutboundChannel())
.get();
}
@Bean
public IntegrationFlow sendToJmsQueue(JmsTemplate wlsJmsTemplate) {
return IntegrationFlows.from(this.jmsOutboundChannel())
.log(LoggingHandler.Level.INFO, "sending to queue", m ->
m.getHeaders().get("Message_Type"))
//.channel(this.jmsOutboundChannel())
// want inboundDataQueue to be determined at runtime
.handle(Jms.outboundAdapter(wlsJmsTemplate).destination(inboundDataQueue), e -> e.advice(expressionAdvice()))
// .handleWithAdapter(adapters ->
// adapters.jms(wlsJmsTemplate).destination("outQueue"))
.get();
}
/**
* @param path
* @param fileExt
* @return watch for files created in configured directory and load then send to
* processing
*/
@Bean
@InboundChannelAdapter(value = "fileInputChannel", poller = @Poller(fixedDelay = "1000"))
public MessageSource<File> fileReadingMessageSource(@Value("${file.poller.path}") final String path,
@Value("${file.poller.fileName-pattern}") final String fileExt) {
CompositeFileListFilter<File> filters = new CompositeFileListFilter<>();
filters.addFilter(new SimplePatternFileListFilter(fileExt));
// filters.addFilter(new AcceptOnceFileListFilter<File>());
FileReadingMessageSource source = new FileReadingMessageSource();
source.setAutoCreateDirectory(false);
source.setDirectory(new File(path));
source.setFilter(filters);
source.setUseWatchService(true);
source.setWatchEvents(WatchEventType.CREATE);
System.out.println(path);
return source;
}
/**
* this method will evaluate the further action on successful or failure delivery to JMS Queue.
* SpEL will rename the the file in Source Directory accordingly
*/
@Bean
public Advice expressionAdvice() {
ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
advice.setSuccessChannelName("success.input");
advice.setOnSuccessExpressionString("headers['file_originalFile'].renameTo(new java.io.File(headers['file_originalFile'].absolutePath + '.done'))");
advice.setFailureChannelName("failure.input");
advice.setOnFailureExpressionString("headers['file_originalFile'].renameTo(new java.io.File(headers['file_originalFile'].absolutePath + '.failed.to.send'))");
advice.setTrapException(true);
return advice;
}
}
@EnableJms
@配置
@组件扫描
@启用自动配置
公共类集成配置{
私有最终记录器Logger=LoggerFactory.getLogger(IntegrationConfig.class);
私人连接工厂;
私有初始化上下文jndi;
//private QueueSession会话=null;
@值(${spring.wls.InboundDataQueue}”)
私有字符串inboundDataQueue;
@值(${spring.wls.InboundRequestedQueue}”)
BoundRequestedQueue中的私有字符串;
@值(${spring.wls.InboundQueryQueue}”)
私有字符串inboundQueryQueue;
@值(${spring.wls.InboundAckQueue}”)
私有字符串inboundAckQueue;
@自动连线
公共文件处理器文件处理器;
@自动连线
公共OutboundGatewayConfig OutboundGatewayConfig;
@豆子
public MessageChannel fileInputChannel(){
返回新的DirectChannel();
}
@豆子
public MessageChannel jmsOutboundChannel(){
返回新的DirectChannel();
}
@豆子
public MessageChannel customErrorChannel(){
返回新的DirectChannel();
}
@豆子
公共文件ToString Transformer文件ToString Transformer(){
返回新文件toString Transformer();
}
@豆子
公共JmsTemplate getJmsTemplate(@Value(${spring.wls.jndiContext}”)字符串上下文,
@值(“${spring.wls.InboundServerURL}”)字符串serverName,
@值(“${spring.wls.inboundConnfactory}”)字符串factoryName){
if(jndi==null){
//创建上下文
Hashtable data=新的Hashtable();
data.put(Context.INITIAL\u Context\u工厂,Context);
data.put(Context.PROVIDER\u URL,serverName);
试一试{
jndi=新的初始上下文(数据);
}捕获(NamingE例外){
LOGGER.error(“初始化JNDI上下文失败:”,e);
}
}
如果(工厂==null){
//查找JMS连接工厂
/*
*factory=(QueueConnectionFactory)PortableRemoteObject.窄带(jndi.lookup(
*factoryName),QueueConnectionFactory.class);
*/
试一试{
factory=(QueueConnectionFactory)(jndi.lookup(factoryName));
}捕获(NamingE例外){
LOGGER.error(“创建Weblogic JNDI连接工厂时出错::”,e);
//抛出新的BeanCreationException(“JmsTemplate”,“未能创建JmsTemplate”,e);
}
}
CachingConnectionFactory CachingConnectionFactory=新的CachingConnectionFactory(工厂);
JmsTemplate wlsJmsTemplate=新JmsTemplate();
wlsJmsTemplate.setConnectionFactory(cachingConnectionFactory);
返回wlsJmsTemplate;
}
@豆子
公共集成流处理文件(){
返回积分流
.from(“文件输入通道”)
.transform(fileToStringTransformer())
.handle(“文件处理器”、“进程”)
.log(LoggingHandler.Level.INFO,“进程文件”,m->m.getHeaders().get(“消息类型”))
.channel(此.jmsOutboundChannel())
.get();
}
@豆子
公共集成流发送到jmsqueue(JmsTemplate wlsJmsTemplate){
返回IntegrationFlows.from(this.jmsOutboundChannel())
.log(LoggingHandler.Level.INFO,“发送到队列”,m->
m、 getHeaders().get(“消息类型”))
//.channel(此.jmsOutboundChannel())
//希望在运行时确定inboundDataQueue
.handle(Jms.outboundAdapter(wlsJmsTemplate).destination(inboundDataQueue),e->e.advice(expressionAdvice())
//.带适配器的手柄(适配器->
//adapters.jms(wlsJmsTemplate.destination(“outQueue”))
.get();
}
/**
*@param路径
*@param fileExt
*@return监视在已配置目录中创建的文件,然后加载并发送到
*加工
*/
@豆子
@InboundChannelAdapter(value=“fileInputChannel”,poller=@poller(fixedDelay=“1000”))
public MessageSource fileReadingMessageSource(@Value(${file.poller.path}”)最终字符串路径,
@值(“${file.poller.fileName pattern}”)最终字符串(fileExt){
CompositeFileListFilter=新的CompositeFileListFilter();
addFilter(新的SimplePatternFileListFilter(fileExt));
//filters.addFilter(新的AcceptOnceFileListFilter());
FileReadingMessageSource=新建FileReadingMessageSource();
source.setAutoCreateDirectory(false);
setDirectory(新文件(路径));
source.setFilter(过滤器);
source.setUseWatchService(true);
setWatchEvents(WatchEventType.CREATE);
System.out.println(路径);
返回源;
}
/**
*此方法将评估成功或失败交付到JMS队列时的进一步操作。
*SpEL将相应地重命名源目录中的文件
*/
@豆子
公众意见表达广告{
ExpressionEvaluationRequestHandlerAdvice通知=新的ExpressionEvaluationRequestHandlerAdvice();
advice.setSuccessChannelName(“success.input”);
advice.setOnSuccessExpressionString(“headers['file_originalFile']]。重命名为(
/**
* Configure a {@link Function} that will be invoked at run time to determine the destination to
* which a message will be sent. Typically used with a Java 8 Lambda expression:
* <pre class="code">
* {@code
* .<Foo>destination(m -> m.getPayload().getState())
* }
* </pre>
* @param destinationFunction the destination function.
* @param <P> the expected payload type.
* @return the current {@link JmsOutboundChannelAdapterSpec}.
* @see JmsSendingMessageHandler#setDestinationName(String)
* @see FunctionExpression
*/
public <P> S destination(Function<Message<P>, ?> destinationFunction) {
this.target.setDestinationExpression(new FunctionExpression<>(destinationFunction));
return _this();
}
.destination(msg -> "jms/" + msg.getHeaders().get("Message_Type", String.class))
.destination(msg -> {
String type = msg.getHeaders().get("MSG_TYPE", String.class);
if (type.startsWith("ORU") {
return "jms/dataqueue";
}
else ...
})