应用程序引发异常:原因:javax.jms.jmsexception:会话的xaresource尚未登记到分布式事务中。
如何修复?
import bitronix.tm.BitronixTransactionManager;
import bitronix.tm.TransactionManagerServices;
import bitronix.tm.resource.jms.PoolingConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.jta.JtaTransactionManager;
import javax.jms.ConnectionFactory;
import javax.transaction.TransactionManager;
@Configuration
@EnableTransactionManagement
public class BitronixConfig {
@Value("${spring.activemq.broker-url}")
private String brokerUrl;
@Bean(name = "bitronixCfg")
public bitronix.tm.Configuration configuredBitronix() {
bitronix.tm.Configuration conf
= TransactionManagerServices.getConfiguration();
conf.setServerId("TanderTask");
conf.setLogPart1Filename("./tx-logs/part1.btm");
conf.setLogPart2Filename("./tx-logs/part2.btm");
return conf;
}
@Bean(name = "bitronixTransactionManager", destroyMethod="shutdown")
//@DependsOn("bitronixCfg")
public BitronixTransactionManager bitronixTransactionManager() throws Throwable {
BitronixTransactionManager bitronixTransactionManager = TransactionManagerServices.getTransactionManager();
//bitronixTransactionManager.setTransactionTimeout(10000);
return bitronixTransactionManager;
}
@Bean(name = "transactionManager")
//@DependsOn("bitronixTransactionManager")
public PlatformTransactionManager transactionManager(TransactionManager bitronixTransactionManager) throws Throwable {
return new JtaTransactionManager(bitronixTransactionManager);
}
@Bean("xaJmsConnectionFactory")
//@DependsOn("bitronixTransactionManager")
public ConnectionFactory connectionFactoryLocal() {
PoolingConnectionFactory btmPoolingConnectionFactory = new PoolingConnectionFactory();
btmPoolingConnectionFactory.setClassName("org.apache.activemq.ActiveMQXAConnectionFactory");
btmPoolingConnectionFactory.setUniqueName("AMQLocal");
btmPoolingConnectionFactory.setMinPoolSize(1);
btmPoolingConnectionFactory.setMaxPoolSize(5);
btmPoolingConnectionFactory.setAllowLocalTransactions(true);
btmPoolingConnectionFactory.getDriverProperties().setProperty("brokerURL", brokerUrl);
btmPoolingConnectionFactory.init();
return btmPoolingConnectionFactory;
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import javax.jms.BytesMessage;
@Service
public class JmsFileListener {
private static Logger logger = LoggerFactory.getLogger(JmsListener.class);
@Transactional
@JmsListener(destination = "${spring.activemq.destination}")
public void receiveXmlFile(BytesMessage bytesMessage) {
try {
byte[] bytes = new byte[(int) bytesMessage.getBodyLength()];
bytesMessage.readBytes(bytes);
String out = new String(bytes);
logger.info(
String.format(" Queue = %s; File contains = %s",
"${spring.activemq.destination}", out)
);
} catch (Exception e) {
logger.error("Error in JmsFileListener class!!", e);
}
}
}
路线.等级
import com.almod.springjta.db.H2Repo;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Route extends RouteBuilder {
@Override
public void configure() throws Exception {
from("file:data?noop=true")
.to("jms:example");
/*.choice()
.when(header("CamelFileName").endsWith(".xml"))
.to("jms:example")
.when(header("CamelFileName").endsWith(".txt"))
.bean(H2Repo.class, "writeInDB")
.to("jms:example")
.otherwise()
.throwException(new Exception(" Wrong extension !"))
.to("jms:invalid-queue")
.end();*/
}
}
应用程序属性
# ActiveMQ
spring.activemq.broker-url=vm://embedded-broker
spring.activemq.destination=example
spring.jms.template.default-destination=example
# H2 Database
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.datasource.schema=db/schema.sql
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.almod</groupId>
<artifactId>springjta</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringJTA</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<apache-camel-version>2.22.1</apache-camel-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-kahadb-store</artifactId>
<version>5.15.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-bitronix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>${apache-camel-version}</version> <!-- use the same version as your Camel core version -->
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>${apache-camel-version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
<version>${apache-camel-version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-stream</artifactId>
<version>${apache-camel-version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-javaconfig</artifactId>
<version>${apache-camel-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-jms -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jms</artifactId>
<version>${apache-camel-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.camel/camel-context -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-context</artifactId>
<version>${apache-camel-version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-mail</artifactId>
<version>${apache-camel-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
日志
消息历史记录
1条答案
按热度按时间smdnsysy1#
--routeid processorid processor elapsed(ms)[route1][route1][file://data?noop=true ][3][route1][to1][jms:example ] [ 2]
堆栈跟踪