Added akka-spring

This commit is contained in:
Staffan Fransson 2009-12-02 21:16:43 +01:00 committed by Michael Kober
parent edf1d9a6eb
commit ef36304f85
12 changed files with 536 additions and 0 deletions

Binary file not shown.

BIN
akka-spring/lib/spring.jar Normal file

Binary file not shown.

82
akka-spring/pom.xml Normal file
View file

@ -0,0 +1,82 @@
<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>
<artifactId>akka-spring</artifactId>
<name>Akka Spring Module</name>
<packaging>jar</packaging>
<parent>
<artifactId>akka</artifactId>
<groupId>se.scalablesolutions.akka</groupId>
<version>0.6</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<artifactId>akka-actors</artifactId>
<groupId>se.scalablesolutions.akka</groupId>
<version>0.6</version>
</dependency>
<dependency>
<artifactId>akka-util-java</artifactId>
<groupId>se.scalablesolutions.akka</groupId>
<version>0.6</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.aspectwerkz</groupId>
<artifactId>aspectwerkz-nodeps-jdk5</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.aspectwerkz</groupId>
<artifactId>aspectwerkz-jdk5</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<includes>
<include>**/*</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>

View file

@ -0,0 +1,24 @@
<?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="akkaService" class="se.scalablesolutions.akka.service.MyService">
</bean>
<bean id="interceptor" class="se.scalablesolutions.akka.interceptor.AkkaSpringInterceptor">
</bean>
<bean id="interceptedService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="akkaService"/>
</property>
<property name="interceptorNames">
<list>
<value>interceptor</value>
</list>
</property>
</bean>
</beans>

View file

@ -0,0 +1,84 @@
package se.scalablesolutions.akka.interceptor;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import se.scalablesolutions.akka.actor.ActiveObjectAspect;
import se.scalablesolutions.akka.actor.AspectInit;
import se.scalablesolutions.akka.actor.AspectInitRegistry;
import se.scalablesolutions.akka.actor.Dispatcher;
import se.scalablesolutions.akka.util.AkkaSpringJoinPointWrapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.PlatformTransactionManager;
public class AkkaSpringInterceptor extends ActiveObjectAspect implements MethodInterceptor,BeanPostProcessor{
private final static String CLASSNAME = "org.springframework.transaction.support.TransactionSynchronizationManager";
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
//Determine transactional status
boolean transactional = getTransactionStatus();
Dispatcher dispatcher = new Dispatcher(transactional);
dispatcher.start();
AspectInitRegistry.register(methodInvocation.getThis(), new AspectInit(
methodInvocation.getThis().getClass(),
dispatcher,
1000));
AkkaSpringJoinPointWrapper asjp = AkkaSpringJoinPointWrapper.createSpringAkkaAspectWerkzWrapper(methodInvocation);
System.out.println("AkkaSpringInterceptor = " + Thread.currentThread());
Object obj = this.invoke(asjp);
dispatcher.stop();
return obj;
}
@Override
public Object postProcessAfterInitialization(Object bean, String arg1)
throws BeansException {
return bean;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String arg1)
throws BeansException {
return bean;
}
/*
* Checks if intercepted Spring bean is in a transaction
*
*/
private boolean getTransactionStatus() {
String status = null;
Boolean hasTransaction = null;
try {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (contextClassLoader != null) {
Class tsmClass = contextClassLoader.loadClass(CLASSNAME);
hasTransaction = (Boolean) tsmClass.getMethod("isActualTransactionActive", null).invoke(null, null);
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
return hasTransaction;
}
}

View file

@ -0,0 +1,19 @@
package se.scalablesolutions.akka.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import se.scalablesolutions.akka.service.MyService;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("AkkaAppConfig.xml");
MyService myService = (MyService)context.getBean("interceptedService");
System.out.println(Thread.currentThread());
myService.getNumbers(777,"vfsh");
}
}

View file

@ -0,0 +1,23 @@
package se.scalablesolutions.akka.service;
import org.springframework.transaction.annotation.Transactional;
//import se.scalablesolutions.akka.annotation.oneway;
public class MyService {
public Integer getNumbers(int aTestNumber, String aText) {
System.out.println("MyService : " + Thread.currentThread());
return new Integer(aTestNumber);
}
//@oneway
public void calculate() {
for (int i = 1; i < 10000; i++) {
System.out.println("i=" + i);
}
}
}

View file

@ -0,0 +1,130 @@
package se.scalablesolutions.akka.util;
import org.aopalliance.intercept.MethodInvocation;
import org.codehaus.aspectwerkz.joinpoint.*;
import org.codehaus.aspectwerkz.joinpoint.management.JoinPointType;
public class AkkaSpringJoinPointWrapper implements JoinPoint{
private MethodInvocation methodInvocation = null;
public MethodInvocation getMethodInvocation() {
return methodInvocation;
}
public void setMethodInvocation(MethodInvocation methodInvocation) {
this.methodInvocation = methodInvocation;
}
public static AkkaSpringJoinPointWrapper createSpringAkkaAspectWerkzWrapper(MethodInvocation methodInvocation){
AkkaSpringJoinPointWrapper asjp = new AkkaSpringJoinPointWrapper();
asjp.setMethodInvocation(methodInvocation);
return asjp;
}
@Override
public Object getCallee() {
System.out.println("public Object getCallee()");
// TODO Auto-generated method stub
return null;
}
@Override
public Object getCaller() {
System.out.println("public Object getCaller() ");
// TODO Auto-generated method stub
return null;
}
@Override
public Rtti getRtti() {
System.out.println("public Rtti getRtti()");
AkkaSpringRttiWrapper asrw = new AkkaSpringRttiWrapper(methodInvocation);
return asrw;
}
@Override
public Object getTarget() {
System.out.println("public Object getTarget() ");
return methodInvocation.getThis();
}
@Override
public Object getThis() {
System.out.println("public Object getThis()");
return methodInvocation.getThis();
}
@Override
public void addMetaData(Object arg0, Object arg1) {
System.out.println("public void addMetaData(Object arg0, Object arg1)");
// TODO Auto-generated method stub
}
@Override
public Class getCalleeClass() {
System.out.println("public Class getCalleeClass()");
// TODO Auto-generated method stub
return null;
}
@Override
public Class getCallerClass() {
System.out.println("public Class getCallerClass() ");
// TODO Auto-generated method stub
return null;
}
@Override
public EnclosingStaticJoinPoint getEnclosingStaticJoinPoint() {
System.out.println("public EnclosingStaticJoinPoint getEnclosingStaticJoinPoint()");
// TODO Auto-generated method stub
return null;
}
@Override
public Object getMetaData(Object arg0) {
System.out.println("public Object getMetaData(Object arg0) ");
// TODO Auto-generated method stub
return null;
}
@Override
public Signature getSignature() {
System.out.println("public Signature getSignature() ");
// TODO Auto-generated method stub
return null;
}
@Override
public Class getTargetClass() {
System.out.println("public Class getTargetClass() ");
// TODO Auto-generated method stub
return null;
}
@Override
public JoinPointType getType() {
System.out.println("public JoinPointType getType()");
// TODO Auto-generated method stub
return null;
}
@Override
public Object proceed() throws Throwable {
System.out.println("public Object proceed()");
return methodInvocation.proceed();
}
public StaticJoinPoint copy() {
// TODO Auto-generated method stub
return null;
}
}

View file

@ -0,0 +1,95 @@
package se.scalablesolutions.akka.util;
import org.aopalliance.intercept.MethodInvocation;
import org.codehaus.aspectwerkz.joinpoint.MethodRtti;
import org.codehaus.aspectwerkz.joinpoint.Rtti;
import java.lang.reflect.Method;
public class AkkaSpringRttiWrapper implements MethodRtti {
private MethodInvocation methodInvocation = null;
public AkkaSpringRttiWrapper(MethodInvocation methodInvocation){
this.methodInvocation = methodInvocation;
}
@Override
public Method getMethod() {
return methodInvocation.getMethod();
}
@Override
public Class getReturnType() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getReturnValue() {
// TODO Auto-generated method stub
return null;
}
@Override
public Class[] getExceptionTypes() {
// TODO Auto-generated method stub
return null;
}
@Override
public Class[] getParameterTypes() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object[] getParameterValues() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setParameterValues(Object[] arg0) {
// TODO Auto-generated method stub
}
@Override
public Rtti cloneFor(Object arg0, Object arg1) {
// TODO Auto-generated method stub
return null;
}
@Override
public Class getDeclaringType() {
// TODO Auto-generated method stub
return null;
}
@Override
public int getModifiers() {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getName() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getTarget() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object getThis() {
// TODO Auto-generated method stub
return null;
}
}

View file

@ -0,0 +1,6 @@
<?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.xsd">
</beans>

View file

@ -0,0 +1,49 @@
package se.scalablesolutions.akka;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import se.scalablesolutions.akka.service.MyService;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
public void testApp()
{
ApplicationContext context = new ClassPathXmlApplicationContext("spring-test-config.xml");
MyService myService = (MyService)context.getBean("interceptedService");
System.out.println(Thread.currentThread());
Object obj = myService.getNumbers(12,"vfsh");
assertEquals(new Integer(12), obj);
}
}

View file

@ -0,0 +1,24 @@
<?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="akkaService" class="se.scalablesolutions.akka.service.MyService">
</bean>
<bean id="interceptor" class="se.scalablesolutions.akka.interceptor.AkkaSpringInterceptor">
</bean>
<bean id="interceptedService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="akkaService"/>
</property>
<property name="interceptorNames">
<list>
<value>interceptor</value>
</list>
</property>
</bean>
</beans>