PDA

View Full Version : Spring Integration w/ DM Server where "new ClassPathXmlApplicationContext()" not work



jazzmann
Sep 30th, 2009, 06:03 AM
Hi,

our standalone application uses Spring Integration.
It listens to a directory in interval.
New files are read and a bean will create some entities in a database rest upon the content of these files.

There is a main application, where the system is started.



package org.foo.solarintegration.main;

import org.springframework.context.support.AbstractApplic ationContext;
import org.springframework.context.support.ClassPathXmlAp plicationContext;

public class App {
static AbstractApplicationContext context;

public static void main(String[] args) {
context = new ClassPathXmlApplicationContext("beans.xml");
}

public static AbstractApplicationContext getContext() {
return context;
}
}



Now with OSGI, in the Spring DM Server, the Web bundle should start the work in the Integration bundle.
First try was: the web bundle calls a method in the integration bundle, wich creates the new context like in the upper code.

But this won't work.
I need something like the following.
The web bundle uses a gateway to the integration bundle.
Via this gateway the integration work should be started.
Here is a good example how to use Spring Integration in OSGI with gateways:
http://blog.springsource.com/2009/07/28/spring-integration-103-samples-just-add-osgi/
The inbound uses a gateway to the outbound wich offers a file:outbound-gateway to write files containing messages send by the inbound.

Here is the XML configuration file in the META-INF/spring folder which is similar to the configuration in the Spring Integration standalone application:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:integration="http://www.springframework.org/schema/integration" xmlns:file="http://www.springframework.org/schema/integration/file"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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
http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file-1.0.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">

<!--
SPRING INTEGRATION
-->
<!-- process property placeholders using a property file -->
<context:property-placeholder location="META-INF/spring/net-env.properties" />

<file:inbound-channel-adapter directory="file:${incoming.directory}" auto-startup="true"
filename-pattern="${incoming.pattern}" id="incoming" channel="files" />

<integration:channel id="files" datatype="java.io.File">
<integration:queue capacity="10" />
</integration:channel>

<!-- Gateway which creates new jobs in the database -->
<bean id="registerJob" class="org.foo.integration.RegisterSample" />

<!-- Call the specified bean with message payloads of the input channel -->
<integration:service-activator input-channel="files" ref="registerJob" method="register" />

<!-- -->
<integration:poller id="poller" default="true" max-messages-per-poll="3">
<integration:interval-trigger interval="2000" />
</integration:poller>


<!-- -->
<osgi:reference id="hibernateSampleDAOImpl" interface="org.foo.SampleDAO" />
</beans>


Is the application context created automativally by DM Server?
This code snippet --auto-startup="true"-- has no effect.

So, how to solve this? :)

King regards
Alex

oleg.zhurakousky
Sep 30th, 2009, 07:10 AM
Alex

I would suggest to read up Spring-DM documentation especially the chapter about ApplicationContext creation: http://static.springsource.org/osgi/docs/1.2.0/reference/html-single/#bnd-app-ctx
OSGi introduces a radically different programming, class-loading and deployment model (remember no more class-path in a normal sense), which means a lot of things needs to be done differently to accommodate this model, that is why we have Spring-DM as a foundation block for dmServer. It uses its own custom implementation of ApplicationContext interface (OsgiBundleXmlApplicationContext). However, before you venture into creating your own Application Context:

Yes, ApplicationContext is created automatically by Spring-DM Extender bundle.
Bundles communicate with one another via OSGi services (as in the example you were mentioning in your post), to accommodate OSGi Service dynamics

So, all you need is two bundles: web and integration.
Integration bundle would export its Gateway as an OSGi service (<osgi:service/>). Such Gateway service would be imported by the web bundle (<osgi:reference/>).
Again, the example from our blogs that you were referring to clearly shows how to accomplish that as it demonstrates two integration bundles communication with one another via OSGi services.

jazzmann
Sep 30th, 2009, 07:30 AM
Thanks for Your fast reply.
I will read the documentation again and will try your proposal.

Alex