Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any repo for annotation based samples ? #151

Closed
radiumx3 opened this issue Nov 17, 2015 · 17 comments
Closed

Any repo for annotation based samples ? #151

radiumx3 opened this issue Nov 17, 2015 · 17 comments

Comments

@radiumx3
Copy link

Any repo for annotation based samples ?

@artembilan
Copy link
Member

Hi @radiumx3 !

Not sure what is your question, but this one can cover any samples related to the Spring Integration.
If you wonder to find some specific sample which reflect some particular use-case just with annotations, you should just ask here and we may consider that as contribution to this repo.

Cheers,
Artem

P.S. Leaving open until your clarification.

@radiumx3
Copy link
Author

We were looking for fully java based sample (no xml, just annotations) (File move, file copy workflow), we didn't manage to find the java equivalent for outbound-adapter xml tag in the beggining, we found out how to do it though. If anyway there is some pure java <-> xml cheatsheet I would be glad to study it :) Thanks anyway

@artembilan
Copy link
Member

See here: http://docs.spring.io/spring-integration/docs/latest-ga/reference/html/files.html#_configuring_with_java_configuration_7
You can find something similar throughout the manual. Not everywhere, of course, but we try to do our best on demand 😄 .

The main concept of Java config is described here: http://docs.spring.io/spring-integration/docs/latest-ga/reference/html/configuration.html#annotations.

Everything else are just Java classes which should be configured as @Bean s with particular Messaging Annotation.
We just didn't think that it is so difficult to get the concrete class from the Framework and follow with the classical Spring Java Config.
In addition consider to study: https://github.com/spring-projects/spring-integration-java-dsl.

@radiumx3
Copy link
Author

As far as I understood, Java DSL make extensive use of Java Lambda (Java8) but we have to stick with java7 (internal constraint)

@garyrussell
Copy link
Contributor

The DSL is "better" (less verbose) when using lambdas, but they are no means required; it fully supports Java 7.

As Artem said, we are adding annotation-based configuration to the reference manual as time permits, we have done a few recently, but generally it's driven by specific user requests.

Same thing with the samples; we just don't have the resources to provide annotation-based versions for them all in one go.

If there is something specific you need help with we can try to accommodate.

Contributions are welcome too.

@Sudheer786
Copy link

Hi Artem/Gary,

below is my Code for Spring Integration , I am trying to poll a folder and I have configured it properly.

@InboundChannelAdapter(value = "inboundchannel", poller = @Poller(fixedDelay = "50000", maxMessagesPerPoll = "1"))
public Message<File> processFiles() {
fileReadingMessageSource = getFileReadingMessageSource();
return fileReadingMessageSource.receive();
}

@ServiceActivator(inputChannel = "inboundchannel")
public void process(Message<File> payload) {
processFile(payload);

}

but once the program initializes..I am getting an Exception as below,

Exception :

Dec 21, 2015 3:22:11 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class com.activiti.servlet.WebConfigurer
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cSVFileProcessor' defined in URL [jar:file:/xjp/appserver_1/tomcat/apache-tomcat-7.0.42/webapps/activiti-app/WEB-INF/lib/nexen-workflow-ext-0.0.3.jar!/com/activiti/extension/bean/CSVFileProcessor.class]: Initialization of bean failed; nested exception is org.springframework.messaging.core.DestinationResolutionException: failed to look up MessageChannel with name 'inboundchannel' in the BeanFactory.; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'inboundchannel' is defined
        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$1.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 com.activiti.servlet.WebConfigurer.contextInitialized(WebConfigurer.java:67)
  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 com.activiti.servlet.WebConfigurer.contextInitialized(WebConfigurer.java:67)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
        at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:656)
        at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1635)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
        at java.util.concurrent.FutureTask.run(FutureTask.java:166)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:722)
Caused by: org.springframework.messaging.core.DestinationResolutionException: failed to look up MessageChannel with name 'inboundchannel' in the BeanFactory.; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'inboundchannel' is defined
        at org.springframework.integration.support.channel.BeanFactoryChannelResolver.resolveDestination(BeanFactoryChannelResolver.java:112)
        at org.springframework.integration.support.channel.BeanFactoryChannelResolver.resolveDestination(BeanFactoryChannelResolver.java:45)
        at org.springframework.integration.config.annotation.InboundChannelAdapterAnnotationPostProcessor.postProcess(InboundChannelAdapterAnnotationPostProcessor.java:64)
        at org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor$1.doWith(MessagingAnnotationPostProcessor.java:177)
        at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:494)
        at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:501)
        at org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor.postProcessAfterInitialization(MessagingAnnotationPostProcessor.java:145)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
        ... 23 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'inboundchannel' is defined
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1168)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:281)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
        at org.springframework.integration.support.channel.BeanFactoryChannelResolver.resolveDestination(BeanFactoryChannelResolver.java:88)
        ... 32 more

will you please help me with this Issue ?

@artembilan
Copy link
Member

No bean named 'inboundchannel' is defined

You have to declare @Bean as a MessageChannel for that name. Otherwise it doesn't work as you see.

From other side it isn't clear to me why you don't use FileReadingMessageSource directly as a:

@Bean
@InboundChannelAdapter(value = "inboundchannel", poller = @Poller(fixedDelay = "50000", maxMessagesPerPoll = "1"))
public MessageSource<File> fileMessageSource() {
new FileReadingMessageSource();
.........
}

http://docs.spring.io/spring-integration/docs/latest-ga/reference/html/configuration.html#annotations

@Sudheer786
Copy link

Hi Artem,

thanks a lot for your quick reply.

I will try by keeping @Bean Annotation and let you know.

to answer your second question , I want to have a single instance of FileReadingMessageSource through out my Application , that's the reason why I am getting it from a method where I created a single Instance for it.

@Sudheer786
Copy link

Hi Artem, I am giving you the entire Annotations I have used at class level to make the Issue clear.

@Component("cSVFileProcessor")
@Configuration
@IntegrationComponentScan
@EnableIntegration
@MessageEndpoint
public class CSVFileProcessor {

public static FileReadingMessageSource getFileReadingMessageSource() {
        try {
            if (fileReadingMessageSource == null) {
                fileReadingMessageSource = new FileReadingMessageSource();
                fileReadingMessageSource.setDirectory(new File(folderPath));
                SimplePatternFileListFilter simplePatternFileListFilter = new SimplePatternFileListFilter(
                        filePattern);
                IgnoreHiddenFileListFilter ignoreHiddenFileListFilter = new IgnoreHiddenFileListFilter();
                List<FileListFilter<File>> fileListFilter = new ArrayList<FileListFilter<File>>();
                fileListFilter.add(simplePatternFileListFilter);
                fileListFilter.add(ignoreHiddenFileListFilter);
                compositeFileListFilter = new CompositeFileListFilter<File>(
                        fileListFilter);
                CustomScanner cs = new CustomScanner();
                cs.setFilter(compositeFileListFilter);
                fileReadingMessageSource.setScanner(cs);
                logger.info("In static method after Initializing frms   "
                        + fileReadingMessageSource);
            }
        } catch (Exception e) {
            logger.debug("Exception while creating FileReadingMessageSource");
            logger.debug("Exception while creating FileReadingMessageSource", e);
            return null;
        }
        return fileReadingMessageSource;
    }

    @InboundChannelAdapter(value = "inboundchannel", poller = @Poller(fixedDelay = "5000", maxMessagesPerPoll = "1"))
    public Message<File> processFiles() {
        fileReadingMessageSource = getFileReadingMessageSource();

        return fileReadingMessageSource.receive();
    }

    @ServiceActivator(inputChannel = "inboundchannel")
    public void process(Message<File> payload) {
        logger.info("In process() ... payload is" + payload.getClass());

            processFile(payload);


    }
}

The above configuration has worked on my local setup perfectly fine .

but the same code when I moved to Dev machine , we are getting the Exception which I have mentioned above.

will you please provide your comments ? do you still want me to add @Bean Annotation.

thanks In Advance.

@artembilan
Copy link
Member

There might be some delay and unexpected order for the bean population to the application context which depends of the platform and the count of CPU, or even Java version.
Having that we don't recommend to use auto channel creating for such kind of configuration from the inbound-channel to the service-activator .

So, or you follow with our recommendation and create the @Bean for the inboundchannel or you continue live with the issue.
But what is the reason to disturb us than ? 😄

@maxiwu
Copy link

maxiwu commented Mar 4, 2016

have same question as radiumx3.
maybe there could be a document that explain how XML are convert to java config?
for example there is <int:outbound-channel-adapter /> but no annotation @OutboundChannelAdapter
If we could understand how these works, we could write java config sample ourselves.

to artembilan, just my experience, I've create backend and rest service with spring mvc using java config. but I am still having a hard time to use spring integration, there are people find it difficult with the current version of documentation.

@radiumx3
Copy link
Author

radiumx3 commented Mar 6, 2016

@maxiwu Well same here, we're working with Spring framework on a daily basis but Spring integration still remain the worst part of it due to a lack of documentation on the Java configuration part (yes we're not into xml configuration at all) 👎

@garyrussell
Copy link
Contributor

We are adding @Configuration and DSL samples as time permits; but most of our time is spent enhancing the framework itself.

I added a PR for an MQTT sample yesterday.

It would be nice if we received more community submissions for this important task.

Everything generally fits the same pattern - @InboundChannelAdapter + MessageSource @Bean for polled adapters, simple @Bean for event-driven adapters and gateways, and @ServiceActivator + MessageHandler for outbound endpoints.

It's generally pretty easy to use the existing docs (that have new java/dsl config) and samples and to look at the XML parsers to see exactly which beans are needed when using @Configuration.

@garyrussell
Copy link
Contributor

It is not at all reasonable (or practical) to ask us to rewrite the entire reference manual, or recreate every sample immediately. But, generally, we try to respond to specific questions such as "how do I configure an 'x' adapter in java", either here or in StackOverflow in a timely manner. We use such questions to drive updates to the reference manual and samples.

So, if you have something specific you need, open a new issue here or ask it on Stack Overflow.

java <-> xml cheatsheet I would be glad to study it :)

The "cheat sheet" is the XML parser source code 😄 which shows every bean that is created when parsing some xml. Each module has a bunch of parsers in the o.s.i.xxx.config package.

@radiumx3
Copy link
Author

radiumx3 commented Mar 7, 2016

"It is not at all reasonable (or practical) to ask us to rewrite the entire reference manual"

Well I admit it is some massive work, but for most of us if we can't find it by RTFM, we try to find workaround here and there and almost always end up using xml (because we cannot post on forums / StackOverflow, at work mainly due to data leakage policies) that's why we're pushing for it.
Anyway thanks for your help.

@artembilan
Copy link
Member

Guys, please, find the JIRA for documentation improvement https://jira.spring.io/browse/INT-3964.
I don't promise to cover there all your particular cases like "move file workflow", but at least we'll share there the basic concepts how to configure Spring Integration with Java & Annotations.

Feel free to comment there your thoughts and ideas!

Closing this one since we aren't going to introduce a new repo and we really and new samples here based on the Annotations and DSL foundations.

@artembilan
Copy link
Member

The reference JIRA (https://jira.spring.io/browse/INT-3964) has been fixed and you can find a new paragraph in the documentation in the latest SNAPSHOT: http://docs.spring.io/spring-integration/docs/4.3.0.BUILD-SNAPSHOT/reference/html/overview.html#programming-tips

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants