Announcement Announcement Module
Collapse
No announcement yet.
Dynamically adding ChannelAdapter to MessageBus Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamically adding ChannelAdapter to MessageBus

    Hi,

    First of all, congrats on the Spring Integration product, it is great.
    Hope someone can advise me on the following.

    Using Spring Integration Milestone 6, I am trying to mimic the following configuration:

    <integration:channel-adapter id="fileInputChannel" source="fileSource">
    <integrationoller period="5000" />
    </integration:channel-adapter>

    <integration:file-source id="fileSource" type="file" directory="d:/temp/in" />

    in code in order to add PollingSource and accompanying ChannelAdapters to the bus at runtime. The code is like this:


    public void addFileSource(String directory, long pollingInterval) {
    Assert.notNull(directory);
    FileSystemResource fsResource = new FileSystemResource(directory);
    FileSource fileSource = new FileSource(fsResource);
    Schedule schedule = new PollingSchedule(pollingInterval);
    InboundChannelAdapter channelAdapter = new InboundChannelAdapter();
    channelAdapter.setInputChannelName("fileInputChann el");
    channelAdapter.setSource(fileSource);
    channelAdapter.setSchedule(schedule);
    // MessageBusAware: bus is injected by Spring container at runtime
    // If bus is running a newly registered endpoint is automatically activated
    messageBus.registerEndpoint(channelAdapter);
    }


    Running this leads to the following stacktrace:

    Exception in thread "main" java.lang.IllegalArgumentException: endpoint name must not be null
    at org.springframework.util.Assert.notNull(Assert.jav a:112)
    at org.springframework.integration.endpoint.DefaultEn dpointRegistry.registerEndpoint(DefaultEndpointReg istry.java:41)
    at org.springframework.integration.bus.DefaultMessage Bus.registerEndpoint(DefaultMessageBus.java:230)
    at com.oracle.healthinsurance.filehandler.OhiMessageB usHandlerImpl.addFileSource(OhiMessageBusHandlerIm pl.java:33)

    How can I set the required name of the endpoint (i.e. the InboundChannelAdapter in my code)? I noticed from the changelog for Spring Integration (changeset 934) that the setName() method was removed from the MessageChannel interface. What is the alternative to use now?

    Thanks,
    - Sjoerd
    Last edited by smichels; Sep 22nd, 2008, 02:08 AM.

  • #2
    setName &gt; setBeanName()

    Hi,

    Using the setBeanName() method I no longer get the error:


    public void addFileSource(String directory, long pollingInterval) {
    Assert.notNull(directory);
    FileSystemResource fsResource = new FileSystemResource(directory);
    FileSource fileSource = new FileSource(fsResource);
    Schedule schedule = new PollingSchedule(pollingInterval);
    InboundChannelAdapter channelAdapter = new InboundChannelAdapter();
    channelAdapter.setInputChannelName("fileInputChann el");
    channelAdapter.setSource(fileSource);
    channelAdapter.setSchedule(schedule);
    channelAdapter.setBeanName("testChannelAdapter");
    // MessageBusAware: bus is injected by Spring container at runtime
    // If bus is running a newly registered endpoint is automatically activated
    messageBus.registerEndpoint(channelAdapter);
    }


    However, the system is not picking up a file that I drop.
    Obviously, my piece of naive code is missing something, i.e. a proper definition of the bean in the Spring application context.

    Any pointers appreciated.

    - Sjoerd

    Comment


    • #3
      How are you creating the MessageBus? If that is also programmatically instantiated, you will need to start(). Normally, it receives an event from the ApplicationContext and will 'auto-startup' by default. If that is not the solution, then perhaps you can provide more examples from your configuration?

      Comment


      • #4
        I've had a similar need here, and even if the message bus was auto-started, to add more channels, endpoints, they've got started only if I cycle the messagebus, aka bus.stop(); bus.start()

        Comment


        • #5
          Creating the MessageBus

          Hi Mark,

          I replied to your questions earlier, but so far my reply did not show up in this thread. Trying again.

          The MessageBus is created with the following config file:

          <?xml version="1.0" encoding="UTF-8" ?>

          <beans default-lazy-init="false" default-autowire="no"
          default-dependency-check="none"
          xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:lang="http://www.springframework.org/schema/lang"
          xmlns:aop="http://www.springframework.org/schema/aop"
          xmlns:context="http://www.springframework.org/schema/context"
          xmlns:integration="http://www.springframework.org/schema/integration"
          xsi:schemaLocation=
          "http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
          http://www.springframework.org/schema/lang
          http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-2.5.xsd
          http://www.springframework.org/schema/integration
          http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
          <context:load-time-weaver />
          <integration:message-bus />

          <integration:channel id="fileInputChannel" />
          <integration:channel id="workInProcess" />
          <integration:channel id="archive" />

          <integration:channel id="errorChannel" />

          <integration:service-activator input-channel="fileInputChannel" output-channel="workInProcess" ref="fileMover" method="moveFileToWip" />

          <integration:service-activator input-channel="workInProcess" output-channel="archive" ref="fileMover" method="performTask" />

          <integration:service-activator input-channel="archive" ref="fileMover" method="moveFileToArchive" />

          <bean id="fileMover" class="com.oracle.healthinsurance.filehandler.File Mover" />

          <bean id="ohiMessageBusHandler" class="com.oracle.healthinsurance.filehandler.OhiM essageBusHandlerImpl" />

          </beans>


          By default the Bus should start itself. Adding the line
          messageBus.start();
          to the code does not make a difference.

          Thanks again.
          Best regards,
          - Sjoerd

          Comment


          • #6
            Hi Qmx: you are right, when I cycle the bus it does work: a file is picked up.
            Maybe an enhancement request is appropriate for this one?

            Comment


            • #7
              I've opened the jira ticket:

              INT-398 (I'm not allowed to post links)

              Comment


              • #8
                Thank you. The link is: http://jira.springframework.org/browse/INT-398

                Comment


                • #9
                  Could you try this again with the HEAD version?

                  Thanks,
                  Mark

                  Comment


                  • #10
                    Hi Mark,

                    I switched from M6 to 1.0.0.CI-SNAPSHOT. It turns out that Spring Integration is under construction ... For one, FileSource and FileTarget have vanished, not clear to me with what (different concepts) these have been replaced.
                    Is updated API documentation published for snapshots?

                    Best regards,
                    - Sjoerd

                    Comment


                    • #11
                      FileSource and FileTarget have been replaced by PollableFileSource and FileWritingMessageConsumer. These are now configurable in XML within the "file" namespace using the "inbound-channel-adapter" and "outbound-channel-adapter" elements. The documentation is being updated for the RC1 release (this week), and in the meantime, you can check out several samples in "org.springframework.integration.samples.filecopy" .

                      Hope that helps.
                      -Mark

                      Comment


                      • #12
                        Hi Mark,

                        Thanks for the swift reply.
                        I figured out the use of the PollableFileSource, e.g.:

                        FileSystemResource fsResource = new FileSystemResource(directory);
                        PollableFileSource fileSource = new PollableFileSource();
                        fileSource.setInputDirectory(fsResource);
                        FileListFilter fileListFilter = new PatternMatchingFileListFilter(fileNamePattern);

                        Still looking for alternatives for things like messageBus.lookupChannel("someChannel") and programmatically creating a channel adapter, e.g.:
                        InboundChannelAdapter inboundChannelAdapter = new InboundChannelAdapter();

                        Maybe I will wait for the RC1 documentation.

                        Thanks,
                        - Sjoerd

                        Comment


                        • #13
                          Code:
                          FileReadingMessageSource => <file:inbound-channel-adapter/> and
                          FileWritingMessageConsumer => <file:outbound-channel-adapter/>
                          You should be able to find that in the latest documentation. If not you can create a documentation issue in JIRA as well. If you run an ant build it wil generate a zip that contains documentation. (you can find the svn url etc on the project page)
                          Last edited by iwein; Nov 13th, 2008, 02:56 AM. Reason: class renamed

                          Comment


                          • #14
                            Hi Mark and Iwein,

                            I can confirm that this works fine in the RC1 release, hooking up a FileReadingMessageSource (source), a MessageChannel, a SourcePollingChannelAdapter (for hooking up the source and the channel), a ServiceActivatingHandler and a EventDrivenConsumer (which actually invokes the handler).

                            Compliments on the RC1 release, I really enjoy it.

                            Cheers,
                            - Sjoerd

                            Comment

                            Working...
                            X