Return to Snippet

Revision: 3901
at October 8, 2007 05:42 by thebugslayer


Updated Code
svn co https://javautilcmdline.svn.sourceforge.net/svnroot/javautilcmdline/branches/mvnstarter/myrunbean

Revision: 3900
at October 2, 2007 15:49 by thebugslayer


Updated Code
svn co https://javautilcmdline.svn.sourceforge.net/svnroot/javautilcmdline/trunk/mvnstarter/myrunbean

Revision: 3899
at October 2, 2007 15:36 by thebugslayer


Updated Code
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>deng</groupId>
	<artifactId>myrunbean</artifactId>
	<name>My Spring RunBean App</name>
	<version>1.0-SNAPSHOT</version>
	<url>http://maven.apache.org</url>
	<!--<repositories>
		<repository>
			<id>artifactory.cnx</id>
			<name>Connextions Repository for Maven2</name>
			<url>http://firebird:8081/artifactory/repo</url>
		</repository>
	</repositories>	
	<pluginRepositories>
		<pluginRepository>
			<id>artifactory.cnx</id>
			<name>Connextions Repository for Maven2</name>
			<url>http://firebird:8081/artifactory/repo</url>
		</pluginRepository>
	</pluginRepositories>-->
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.3.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
			<version>2.0.6</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.4.3</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>0.9.8</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl104-over-slf4j</artifactId>
			<version>1.4.3</version>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.3</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.5</source>
					<target>1.5</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<descriptors>
						<descriptor>src/assemble/binary.xml</descriptor>
					</descriptors>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

src/assemble/binary.xml:
<assembly>
	<id>binary</id>
	<formats>
		<format>zip</format>
	</formats>

	<dependencySets>
		<dependencySet>
			<outputDirectory>lib</outputDirectory>
		</dependencySet>
	</dependencySets>
	<fileSets>
		<fileSet>
			<includes>
				<include>bin/*</include>
			</includes>
			<fileMode>755</fileMode>
		</fileSet>
		<fileSet>
			<includes>
				<include>doc/*</include>
				<include>conf/*</include>
			</includes>
		</fileSet>
		<fileSet>
			<directory>target</directory>
			<outputDirectory>lib</outputDirectory>
			<includes>
				<include>*.jar</include>
			</includes>
		</fileSet>
	</fileSets>
</assembly>

conf/beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
	
	<bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="file:${appHome}/conf/beans.properties"></property>
	</bean>
	
	<bean id="main" class="deng.myrunbean.RunnableBeanExecutor" init-method="init" destroy-method="destroy">
		<property name="taskExecutor">
			<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor"></bean>
		</property>
		<property name="runnableBeans">
			<list>
				<ref bean="myApp"/>
			</list>
		</property>
	</bean>

	<bean id="myApp" class="deng.myrunbean.MyApp">
		<property name="message" value="Hello World!"></property>
	</bean>
</beans>

src/main/java/deng/myrunbean/RunBean.java:
package deng.myrunbean;

import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * Bootstrap a Spring ApplciationContext and invoke a Runnable Bean.
 *  
 * Usage:
 *  mvn -q exec:java -DappHome=. -Dexec.mainClass=deng.myrunbean.RunBean -Dexec.args="conf/beans.xml main" 
 * 
 * @author zemian
 * 
 */
public class RunBean {
	public static void main(String[] args) {
		Logger logger = LoggerFactory.getLogger(RunBean.class);
		logger.debug("Command arguments " + Arrays.asList(args));

		String config = "conf/beans.xml";
		String mainBeanName = "main";

		if (args.length >= 1) {
			config = args[0];
			
			// check for help page.
			if(config.toLowerCase().equals("h") || config.toLowerCase().equals("help")){
				logger.info("Usage: java RunBean [configFile] [beanName]");
				logger.info("  Default configFile = conf/beans.xml, beanName=main");
				System.exit(0);
			}
		}
		if (args.length >= 2) {
			mainBeanName = args[1];
		}

		logger.debug("Loading " + config);
		FileSystemXmlApplicationContext applicationContext = new FileSystemXmlApplicationContext(config);
		applicationContext.registerShutdownHook();

		Runnable bean = (Runnable) applicationContext.getBean(mainBeanName);
		logger.info("Run bean [" + mainBeanName + "] " + bean);

		try {
			bean.run();
		}
		catch (RuntimeException e) {
			logger.error("Run error.", e);
		}

		//logger.info("Done.");
	}
}


src/main/java/deng/myrunbean/RunnableBean.java:
package deng.myrunbean;


/** 
 * @author zemian
 * 
 */
public interface RunnableBean extends Runnable {
	
	public void init() throws Exception;

	public void destroy() throws Exception;
}

src/main/java/deng/myrunbean/RunnableBeanExecutor.java:
package deng.myrunbean;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.task.TaskExecutor;

/**
 * A wrapper bean that will execute a list of runnable beans.
 * 
 * @author zemian
 *
 */
public class RunnableBeanExecutor implements RunnableBean {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	protected TaskExecutor taskExecutor;
	protected List<RunnableBean> runnableBeans;
	
	public void setTaskExecutor(TaskExecutor taskExecutor) {
		this.taskExecutor = taskExecutor;
	}
	
	public void setRunnableBeans(List<RunnableBean> runnableBeans) {
		this.runnableBeans = runnableBeans;
	}
	
	public void run() {
		for(RunnableBean bean : runnableBeans){
			logger.info("Executing runnableBean " + bean);
			taskExecutor.execute(bean);
		}
	}

	public void destroy() throws Exception {
		//call in reverse order.
		for(int i = runnableBeans.size()-1; i>=0; i--){
			RunnableBean bean = runnableBeans.get(i);
			logger.info("Destroying runnableBean " + bean);
			bean.destroy();
		}
	}

	public void init() throws Exception {
		for(RunnableBean bean : runnableBeans){
			logger.info("Initializing runnableBean " + bean);
			bean.init();
		}
	}
}


src/main/java/deng/myrunbean/MyApp.java:
package deng.myrunbean;
import java.io.*;
import java.util.*;
import org.slf4j.*;

public class MyApp implements RunnableBean {
	protected Logger logger = LoggerFactory.getLogger(getClass());
        protected String message;
        
        public void setMessage(String msg){
            message = msg;
        }

	public void run(){
		logger.info(message);
	}
        public void init() throws Exception{
		logger.info("Initialized.");
	}

	public void destroy() throws Exception{
		logger.info("Destroyed.");
	}
}

Revision: 3898
at October 1, 2007 08:50 by thebugslayer


Initial Code
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>deng</groupId>
	<artifactId>myrunbean</artifactId>
	<name>My Spring RunBean App</name>
	<version>1.0-SNAPSHOT</version>
	<url>http://maven.apache.org</url>
	<!--<repositories>
		<repository>
			<id>artifactory.cnx</id>
			<name>Connextions Repository for Maven2</name>
			<url>http://firebird:8081/artifactory/repo</url>
		</repository>
	</repositories>	
	<pluginRepositories>
		<pluginRepository>
			<id>artifactory.cnx</id>
			<name>Connextions Repository for Maven2</name>
			<url>http://firebird:8081/artifactory/repo</url>
		</pluginRepository>
	</pluginRepositories>-->
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.3.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
			<version>2.0.6</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.4.3</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>0.9.8</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl104-over-slf4j</artifactId>
			<version>1.4.3</version>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.3</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.5</source>
					<target>1.5</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<descriptors>
						<descriptor>src/assemble/binary.xml</descriptor>
					</descriptors>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

src/assemble/binary.xml:
<assembly>
	<id>binary</id>
	<formats>
		<format>zip</format>
	</formats>

	<dependencySets>
		<dependencySet>
			<outputDirectory>lib</outputDirectory>
		</dependencySet>
	</dependencySets>
	<fileSets>
		<fileSet>
			<includes>
				<include>bin/*</include>
			</includes>
			<fileMode>755</fileMode>
		</fileSet>
		<fileSet>
			<includes>
				<include>doc/*</include>
				<include>conf/*</include>
			</includes>
		</fileSet>
		<fileSet>
			<directory>target</directory>
			<outputDirectory>lib</outputDirectory>
			<includes>
				<include>*.jar</include>
			</includes>
		</fileSet>
	</fileSets>
</assembly>

conf/beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
	
	<bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="file:${appHome}/conf/beans.properties"></property>
	</bean>
	
	<bean id="main" class="deng.myrunbean.RunnableBeanExecutor" init-method="init" destroy-method="destroy">
		<property name="taskExecutor">
			<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor"></bean>
		</property>
		<property name="runnableBeans">
			<list>
				<ref bean="myApp"/>
			</list>
		</property>
	</bean>

	<bean id="myApp" class="deng.myrunbean.MyApp">
		<property name="message" value="Hello World!"></property>
	</bean>
</beans>

src/main/java/deng/myrunbean/RunBean.java:
package deng.myrunbean;

import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * Bootstrap a Spring ApplciationContext and invoke a Runnable Bean.
 *  
 * Usage:
 *  mvn -q exec:java -DappHome=. -Dexec.mainClass=cnx.aspect.client.cli.RunBean -Dexec.args="conf/beans.xml main" 
 * 
 * @author zemian
 * 
 */
public class RunBean {
	public static void main(String[] args) {
		Logger logger = LoggerFactory.getLogger(RunBean.class);
		logger.debug("Command arguments " + Arrays.asList(args));

		String config = "conf/beans.xml";
		String mainBeanName = "main";

		if (args.length >= 1) {
			config = args[0];
			
			// check for help page.
			if(config.toLowerCase().equals("h") || config.toLowerCase().equals("help")){
				logger.info("Usage: java RunBean [configFile] [beanName]");
				logger.info("  Default configFile = conf/beans.xml, beanName=main");
				System.exit(0);
			}
		}
		if (args.length >= 2) {
			mainBeanName = args[1];
		}

		logger.debug("Loading " + config);
		FileSystemXmlApplicationContext applicationContext = new FileSystemXmlApplicationContext(config);
		applicationContext.registerShutdownHook();

		Runnable bean = (Runnable) applicationContext.getBean(mainBeanName);
		logger.info("Run bean [" + mainBeanName + "] " + bean);

		try {
			bean.run();
		}
		catch (RuntimeException e) {
			logger.error("Run error.", e);
		}

		//logger.info("Done.");
	}
}


src/main/java/deng/myrunbean/RunnableBean.java:
package deng.myrunbean;


/**
 * Base class that provide atomic runnable variable check, and provide
 * run, init and destroy methods. Sub class is responsible to set the
 * isRunning flag.
 * 
 * @author zemian
 * 
 */
public interface RunnableBean extends Runnable {
	
	public void init() throws Exception;

	public void destroy() throws Exception;
}

src/main/java/deng/myrunbean/RunnableBeanExecutor.java:
package deng.myrunbean;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.task.TaskExecutor;

/**
 * A wrapper bean that will execute a list of runnable beans.
 * 
 * @author zemian
 *
 */
public class RunnableBeanExecutor implements RunnableBean {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	protected TaskExecutor taskExecutor;
	protected List<RunnableBean> runnableBeans;
	
	public void setTaskExecutor(TaskExecutor taskExecutor) {
		this.taskExecutor = taskExecutor;
	}
	
	public void setRunnableBeans(List<RunnableBean> runnableBeans) {
		this.runnableBeans = runnableBeans;
	}
	
	public void run() {
		for(RunnableBean bean : runnableBeans){
			logger.info("Executing runnableBean " + bean);
			taskExecutor.execute(bean);
		}
	}

	public void destroy() throws Exception {
		//call in reverse order.
		for(int i = runnableBeans.size()-1; i>=0; i--){
			RunnableBean bean = runnableBeans.get(i);
			logger.info("Destroying runnableBean " + bean);
			bean.destroy();
		}
	}

	public void init() throws Exception {
		for(RunnableBean bean : runnableBeans){
			logger.info("Initializing runnableBean " + bean);
			bean.init();
		}
	}
}


src/main/java/deng/myrunbean/MyApp.java:
package deng.myrunbean;
import java.io.*;
import java.util.*;

public class MyApp implements RunnableBean {
	protected Logger logger = LoggerFactory.getLogger(getClass());
        protected String message;
        
        public void setMessage(String msg){
            message = msg;
        }

	public void run(){
		logger.info(message);
	}
}

Initial URL

                                

Initial Description

                                

Initial Title
spring-runbean standalone app setup: pom.xml, beans.xml, assembly.xml

Initial Tags

                                

Initial Language
Java