Skip to content
Permalink
Browse files
BATCHEE-30 add an @asynchronous Thread executor and a UT TransactionS…
…ervice
  • Loading branch information
struberg committed May 7, 2014
1 parent 8cbed5e commit e6aae06470ce6742e80aff1a9f475c019e55cbc0
Show file tree
Hide file tree
Showing 7 changed files with 294 additions and 1 deletion.
@@ -48,6 +48,10 @@
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_1.0_spec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-ejb_3.1_spec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-interceptor_1.1_spec</artifactId>
@@ -80,9 +80,13 @@ public static InjectionReferences setInjectionReferences(InjectionReferences inj
}

public static <T> T createProxy(T delegate, InjectionReferences injectionRefs, String... nonExceptionHandlingMethods) {
if (delegate == null) {
// this is allowed per the spec! But don't ask why...
return null;
}

return (T) Proxy.newProxyInstance(delegate.getClass().getClassLoader(), getInterfaces(delegate.getClass()),
new BatchProxyInvocationHandler(delegate, injectionRefs, nonExceptionHandlingMethods));

}


@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.batchee.container.services.executor.ee;

import java.util.Properties;
import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;

import org.apache.batchee.container.cdi.BatchCDIInjectionExtension;
import org.apache.batchee.spi.BatchThreadPoolService;

/**
* Activate this class in your META-INF/batchee.properties as
* <pre>
* BatchThreadPoolService=org.apache.batchee.container.services.executor.ee.AsyncEjbBatchThreadPoolService
* </pre>
*/
public class AsyncEjbBatchThreadPoolService implements BatchThreadPoolService {

private BeanManager beanManager;
private ThreadExecutorEjb threadExecutorEjb;

@Override
public void init(Properties batchConfig) {
beanManager = BatchCDIInjectionExtension.getInstance().getBeanManager();

Set<Bean<?>> beans = beanManager.getBeans(ThreadExecutorEjb.class);
Bean<?> bean = beanManager.resolve(beans);
CreationalContext cc = beanManager.createCreationalContext(bean);

threadExecutorEjb = (ThreadExecutorEjb) beanManager.getReference(bean, bean.getBeanClass(), cc);
}

@Override
public void executeTask(Runnable work, Object config) {
threadExecutorEjb.executeTask(work, config);
}

@Override
public void shutdown() {
// X TODO
}
}
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.batchee.container.services.executor.ee;

import javax.annotation.Resource;
import javax.ejb.Asynchronous;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.transaction.UserTransaction;

@Singleton
@Lock(LockType.READ)
@TransactionManagement(TransactionManagementType.BEAN)
public class ThreadExecutorEjb {

/**
* Used for the TransactionService
*/
@Resource
private UserTransaction ut;

private static ThreadLocal<UserTransaction> userTransactions = new ThreadLocal<UserTransaction>();

@Asynchronous
public void executeTask(Runnable work, Object config) {
try {
userTransactions.set(ut);
work.run();
} finally {
userTransactions.remove();
}
}


public static UserTransaction getUserTransaction() {
return userTransactions.get();
}
}
@@ -0,0 +1,105 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.batchee.container.services.executor.ee;

import java.util.Properties;

import javax.batch.runtime.context.StepContext;
import javax.transaction.UserTransaction;

import org.apache.batchee.container.util.ExceptionUtil;
import org.apache.batchee.spi.TransactionManagementService;
import org.apache.batchee.spi.TransactionManagerAdapter;

/**
* @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
*/
public class UserTransactionTransactionService implements TransactionManagementService {
@Override
public void init(Properties batchConfig) {

}

@Override
public TransactionManagerAdapter getTransactionManager(StepContext stepContext) {
UserTransaction ut = ThreadExecutorEjb.getUserTransaction();

return null;
}

private static class UserTransactionTxAdapter implements TransactionManagerAdapter {
private final UserTransaction ut;

private UserTransactionTxAdapter(UserTransaction ut) {
this.ut = ut;
}

@Override
public void begin() {
try {
ut.begin();
} catch (Exception e) {
ExceptionUtil.throwAsRuntimeException(e);
}
}

@Override
public void commit() {
try {
ut.commit();
} catch (Exception e) {
ExceptionUtil.throwAsRuntimeException(e);
}
}

@Override
public int getStatus() {
try {
return ut.getStatus();
} catch (Exception e) {
throw ExceptionUtil.throwAsRuntimeException(e);
}
}

@Override
public void rollback() {
try {
ut.rollback();
} catch (Exception e) {
ExceptionUtil.throwAsRuntimeException(e);
}
}

@Override
public void setRollbackOnly() {
try {
ut.setRollbackOnly();
} catch (Exception e) {
ExceptionUtil.throwAsRuntimeException(e);
}
}

@Override
public void setTransactionTimeout(int timeout) {
try {
ut.setTransactionTimeout(timeout);
} catch (Exception e) {
ExceptionUtil.throwAsRuntimeException(e);
}
}
}
}
@@ -0,0 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.batchee.container.util;

/**
* This class is taken from Apache OpenWebBeans.
*
* It instantly re-throws any Exception as if it would be a RuntimeException
*/
public class ExceptionUtil {
private ExceptionUtil() {
// prevent instantiation
}

public static RuntimeException throwAsRuntimeException(Throwable throwable) {
//Attention: helper which allows to use a trick to throw
// a catched checked exception without a wrapping exception
new ExceptionHelper<RuntimeException>().throwException(throwable);

return null; //not needed due to the helper trick, but it's easier for using it
}

@SuppressWarnings({"unchecked"})
private static class ExceptionHelper<T extends Throwable> {
private void throwException(Throwable exception) throws T {
try {
//exception-type is only checked at compile-time
throw (T) exception;
} catch (ClassCastException e) {
//doesn't happen with existing JVMs! - if that changes the local ClassCastException needs to be ignored
//-> throw original exception
if (e.getStackTrace()[0].toString().contains(getClass().getName())) {
if (exception instanceof RuntimeException) {
throw (RuntimeException) exception;
}
throw new RuntimeException(exception);
}
//if the exception to throw is a ClassCastException, throw it
throw e;
}
}
}
}
@@ -132,6 +132,12 @@
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-ejb_3.1_spec</artifactId>
<version>1.0.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-interceptor_1.1_spec</artifactId>

0 comments on commit e6aae06

Please sign in to comment.