Skip to content

Instantly share code, notes, and snippets.

@tyb
Last active July 25, 2019 11:00
Show Gist options
  • Save tyb/9d80b2a4324f0c166a8417ef76aa8635 to your computer and use it in GitHub Desktop.
Save tyb/9d80b2a4324f0c166a8417ef76aa8635 to your computer and use it in GitHub Desktop.
common module of multiple services: after environments set use DependsOn
package com.xxx.common.util;
import com.atomikos.icatch.config.UserTransactionServiceImp;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
public class InitializingBeanImpl implements InitializingBean {
@Autowired
private Environment environment;
private String applicationName;
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("afterProperties set: " + Arrays.asList(environment.getProperty("spring.application.name")));
setApplicationName(environment.getProperty("spring.application.name"));
}
public String getApplicationName() {
return applicationName;
}
public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}
}
---
package com.xxx.common.config;
import com.atomikos.icatch.config.UserTransactionServiceImp;
import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;
import net.sf.ehcache.transaction.manager.TransactionManagerLookup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.env.Environment;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
import java.util.Properties;
@Configuration
@DependsOn("initializingBeanImpl")
public class AtomikosJtaConfiguration {
@Autowired
private Environment environment ;
@Value("${spring.application.name}")
private String APPLICATION_NAME;
public void tailorProperties(Properties properties) {
properties.setProperty( "hibernate.transaction.manager_lookup_class",
TransactionManagerLookup.class.getName());
}
@Bean(name="userTransactionServiceImp", initMethod = "init", destroyMethod = "shutdownForce")
public UserTransactionServiceImp userTransactionServiceImp()
{
Properties properties = new Properties();
properties.setProperty("com.atomikos.icatch.max_timeout", "3600000");
properties.setProperty("com.atomikos.icatch.service", "com.atomikos.icatch.standalone.UserTransactionServiceFactory");
properties.setProperty("com.atomikos.icatch.log_base_name", APPLICATION_NAME);
properties.setProperty("com.atomikos.icatch.output_dir", "../standalone/log/");
properties.setProperty("com.atomikos.icatch.log_base_dir", "../standalone/log/");
UserTransactionServiceImp userTransactionServiceImp = new UserTransactionServiceImp(properties);
return userTransactionServiceImp;
}
//@Bean(initMethod = "init", destroyMethod = "close", name = "userTransaction")
@DependsOn("userTransactionServiceImp")
@Bean(name = "userTransaction")
public UserTransaction userTransaction() throws Throwable {
UserTransactionImp userTransactionImp = new UserTransactionImp();
userTransactionImp.setTransactionTimeout(1000);
return userTransactionImp;
}
@Bean(initMethod = "init", destroyMethod = "close", name = "transactionManager")
@DependsOn("userTransactionServiceImp")
//@Bean(name = "transactionManager")
public TransactionManager transactionManager() throws Throwable {
UserTransactionManager userTransactionManager = new UserTransactionManager();
userTransactionManager.setForceShutdown(false);
return userTransactionManager;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment