使用 spring 集成解压缩
Unzip with spring integration
我想听一个目录,读入新的 .zip 文件,在最好的情况下将它传递给 @ServiceActivator,然后将其解压缩到一个目录。在最坏的情况下,我至少想从读取的 zip 文件中获取绝对路径,并在该路径上调用 java unzip util 来解压缩它。
这是我的 InboundChannelAdapter:
@Bean
@InboundChannelAdapter(value = "requestChannel", poller = @Poller(fixedDelay = "100"))
public FileReadingMessageSource adapter() {
FileReadingMessageSource source = new FileReadingMessageSource();
source.setDirectory(new File("path/to/zip/directory"));
return source;
}
这会读入新的 .zips 并将它们传递下去。我已经尝试过一个小写手,将它们写入另一个目录,效果很好。
@Bean
@ServiceActivator(inputChannel = "requestChannel" )
public boolean unzipMe() {
byte[] buffer = new byte[1024];
try {
ZipInputStream zis = new ZipInputStream(new FileInputStream("path/to/zip"));
ZipEntry ze = zis.getNextEntry();
while(ze!=null){
String fileName = ze.getName();
File outPut = new File("path/out/directory" + File.separator + fileName);
FileOutputStream fos = new FileOutputStream(outPut);
int len;
System.out.println("output " + outPut.toString());
while((len = zis.read(buffer))> 0){
fos.write(buffer, 0, len);
}
fos.close();
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
这是我的解压缩方法,它应该被来自 requestChannel 的消息调用,并解压缩文件。目前我将路径硬编码到 .zip,因为我无法获取到达此处的消息。此实现也给了我这个错误,我不明白。
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'integrationConfiguration' defined in file [D:\Workspaces\Integrationnnn\target\classes\demo\IntegrationConfiguration.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Found ambiguous parameter type [class java.lang.Boolean] for method match: [public static boolean java.lang.Boolean.parseBoolean(java.lang.String), public static java.lang.Boolean java.lang.Boolean.valueOf(boolean), public boolean java.lang.Boolean.booleanValue()]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at demo.IntegrationnnnApplication.main(IntegrationnnnApplication.java:12)
Caused by: java.lang.IllegalArgumentException: Found ambiguous parameter type [class java.lang.Boolean] for method match: [public static boolean java.lang.Boolean.parseBoolean(java.lang.String), public static java.lang.Boolean java.lang.Boolean.valueOf(boolean), public boolean java.lang.Boolean.booleanValue()]
at org.springframework.util.Assert.isNull(Assert.java:89)
at org.springframework.integration.util.MessagingMethodInvokerHelper.findHandlerMethodsForTarget(MessagingMethodInvokerHelper.java:446)
at org.springframework.integration.util.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:192)
at org.springframework.integration.util.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:136)
at org.springframework.integration.util.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:131)
at org.springframework.integration.handler.MethodInvokingMessageProcessor.<init>(MethodInvokingMessageProcessor.java:57)
at org.springframework.integration.handler.ServiceActivatingHandler.<init>(ServiceActivatingHandler.java:37)
at org.springframework.integration.config.annotation.ServiceActivatorAnnotationPostProcessor.createHandler(ServiceActivatorAnnotationPostProcessor.java:65)
at org.springframework.integration.config.annotation.AbstractMethodAnnotationPostProcessor.postProcess(AbstractMethodAnnotationPostProcessor.java:117)
at org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor.doWith(MessagingAnnotationPostProcessor.java:151)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:496)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:503)
at org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor.postProcessAfterInitialization(MessagingAnnotationPostProcessor.java:131)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1571)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
... 13 more
然而,这确实可靠地将 .zip 文件解压缩到正确的目录中,只是之后崩溃得很厉害。
简单的解决方案是实现我自己的 MessageHandler 并覆盖它的 handleMessageInternal 方法。
我想听一个目录,读入新的 .zip 文件,在最好的情况下将它传递给 @ServiceActivator,然后将其解压缩到一个目录。在最坏的情况下,我至少想从读取的 zip 文件中获取绝对路径,并在该路径上调用 java unzip util 来解压缩它。
这是我的 InboundChannelAdapter:
@Bean
@InboundChannelAdapter(value = "requestChannel", poller = @Poller(fixedDelay = "100"))
public FileReadingMessageSource adapter() {
FileReadingMessageSource source = new FileReadingMessageSource();
source.setDirectory(new File("path/to/zip/directory"));
return source;
}
这会读入新的 .zips 并将它们传递下去。我已经尝试过一个小写手,将它们写入另一个目录,效果很好。
@Bean
@ServiceActivator(inputChannel = "requestChannel" )
public boolean unzipMe() {
byte[] buffer = new byte[1024];
try {
ZipInputStream zis = new ZipInputStream(new FileInputStream("path/to/zip"));
ZipEntry ze = zis.getNextEntry();
while(ze!=null){
String fileName = ze.getName();
File outPut = new File("path/out/directory" + File.separator + fileName);
FileOutputStream fos = new FileOutputStream(outPut);
int len;
System.out.println("output " + outPut.toString());
while((len = zis.read(buffer))> 0){
fos.write(buffer, 0, len);
}
fos.close();
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
这是我的解压缩方法,它应该被来自 requestChannel 的消息调用,并解压缩文件。目前我将路径硬编码到 .zip,因为我无法获取到达此处的消息。此实现也给了我这个错误,我不明白。
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'integrationConfiguration' defined in file [D:\Workspaces\Integrationnnn\target\classes\demo\IntegrationConfiguration.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Found ambiguous parameter type [class java.lang.Boolean] for method match: [public static boolean java.lang.Boolean.parseBoolean(java.lang.String), public static java.lang.Boolean java.lang.Boolean.valueOf(boolean), public boolean java.lang.Boolean.booleanValue()]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at demo.IntegrationnnnApplication.main(IntegrationnnnApplication.java:12)
Caused by: java.lang.IllegalArgumentException: Found ambiguous parameter type [class java.lang.Boolean] for method match: [public static boolean java.lang.Boolean.parseBoolean(java.lang.String), public static java.lang.Boolean java.lang.Boolean.valueOf(boolean), public boolean java.lang.Boolean.booleanValue()]
at org.springframework.util.Assert.isNull(Assert.java:89)
at org.springframework.integration.util.MessagingMethodInvokerHelper.findHandlerMethodsForTarget(MessagingMethodInvokerHelper.java:446)
at org.springframework.integration.util.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:192)
at org.springframework.integration.util.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:136)
at org.springframework.integration.util.MessagingMethodInvokerHelper.<init>(MessagingMethodInvokerHelper.java:131)
at org.springframework.integration.handler.MethodInvokingMessageProcessor.<init>(MethodInvokingMessageProcessor.java:57)
at org.springframework.integration.handler.ServiceActivatingHandler.<init>(ServiceActivatingHandler.java:37)
at org.springframework.integration.config.annotation.ServiceActivatorAnnotationPostProcessor.createHandler(ServiceActivatorAnnotationPostProcessor.java:65)
at org.springframework.integration.config.annotation.AbstractMethodAnnotationPostProcessor.postProcess(AbstractMethodAnnotationPostProcessor.java:117)
at org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor.doWith(MessagingAnnotationPostProcessor.java:151)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:496)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:503)
at org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor.postProcessAfterInitialization(MessagingAnnotationPostProcessor.java:131)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1571)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
... 13 more
然而,这确实可靠地将 .zip 文件解压缩到正确的目录中,只是之后崩溃得很厉害。
简单的解决方案是实现我自己的 MessageHandler 并覆盖它的 handleMessageInternal 方法。