gpt4 book ai didi

spring-boot - 使用 Spring boot 和集成 DSL,出现错误 ClassNotFoundException integration.history.TrackableComponent

转载 作者:行者123 更新时间:2023-12-02 01:30:16 25 4
gpt4 key购买 nike

使用 Spring Boot、Integration 和 DSL 尝试一个非常基本的 JMS 接收器。我曾研究过基于 Spring Integration 的 XML,但我对 Spring Boot 和 DSL 还是陌生的。

这是我到目前为止的代码示例

@SpringBootApplication
@IntegrationComponentScan
@EnableJms
public class JmsReceiver {

static String mailboxDestination = "RETRY.QUEUE";


@Configuration
@EnableJms
@IntegrationComponentScan
@EnableIntegration
public class MessageReceiver {

@Bean
public IntegrationFlow jmsMessageDrivenFlow() {
return IntegrationFlows
.from(Jms.messageDriverChannelAdapter(this.connectionFactory())
.destination(mailboxDestination))
.transform((String s) -> s.toUpperCase())
.get();
}
//for sneding message
@Bean
ConnectionFactory connectionFactory() {
ActiveMQConnectionFactory acFac = new ActiveMQConnectionFactory();
acFac.setBrokerURL("tcp://crsvcdevlnx01:61616");
acFac.setUserName("admin");
acFac.setPassword("admin");
return new CachingConnectionFactory(acFac);
}
}


//Message send code
public static void main(String args[]) throws Throwable {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(JmsReceiver.class);

JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
System.out.println("Sending a new mesage.");

MessageCreator messageCreator = new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("ping!");
}
};
jmsTemplate.send(mailboxDestination, messageCreator);

context.close();
}
}

而且,使用 Gradle 运行时出现此错误。
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.integration.dsl.IntegrationFlow]: Factory method 'inboundFlow' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/integration/history/TrackableComponent
reflect.NativeMethodAccessorImpl.invoke0(Native Method)
.
.
.
Caused by: java.lang.ClassNotFoundException: org.springframework.integration.history.TrackableComponent
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

我的 gradle 依赖项:
compile "org.springframework.boot:spring-boot-starter-jersey",
"org.springframework.boot:spring-boot-starter-actuator",
"org.springframework.boot:spring-boot-configuration-processor",
"org.springframework.boot:spring-boot-starter-integration",
"org.springframework.integration:spring-integration-jms",
"org.springframework.integration:spring-integration-java-dsl:1.1.1.RELEASE",
"org.springframework.integration:spring-integration-flow:1.0.0.RELEASE",
"org.springframework.integration:spring-integration-core:4.2.2.RELEASE",
"org.springframework.integration:spring-integration-java-dsl:1.1.0.RELEASE",
"org.springframework.integration:spring-integration-flow:1.0.0.RELEASE",
"org.apache.activemq:activemq-spring:5.11.2",

更新.. 已解决:非常感谢。 改变了两件事:
  • 根据您的建议清理 gradle 依赖项。新的看起来像这样:
    compile "org.springframework.boot:spring-boot-starter-jersey",

    "org.springframework.boot:spring-boot-starter-actuator",
    "org.springframework.boot:spring-boot-configuration-processor",
    "org.springframework.boot:spring-boot-starter-integration",
    "org.springframework.integration:spring-integration-jms",
    "org.springframework.integration:spring-integration-java-dsl:1.1.0.RELEASE",
    "org.apache.activemq:activemq-spring:5.11.2"
  • 代码抛出关于无法实例化的构造函数错误 <init>在内部类。将内部类更改为静态。新代码:
    @SpringBootApplication
    @IntegrationComponentScan
    @EnableJms
    public class JmsReceiver {

    static String lsamsErrorQueue = "Queue.LSAMS.retryMessage";
    static String fatalErrorsQueue = "Queue.LSAMS.ManualCheck";

    //receiver
    @EnableJms
    @EnableIntegration
    @Configuration
    public static class MessageReceiver {
    @Bean
    public IntegrationFlow jmsMessageDrivenFlow() {
    return IntegrationFlows
    .from(Jms.messageDriverChannelAdapter(this.connectionFactory())
    .destination(lsamsErrorQueue))
    //call LSAMS REST service with the payload received
    .transform((String s) -> s.toUpperCase())

    .handle(Jms.outboundGateway(this.connectionFactory())
    .requestDestination(fatalErrorsQueue))
    .get();
    }
    @Bean
    ConnectionFactory connectionFactory() {
    ActiveMQConnectionFactory acFac = new ActiveMQConnectionFactory();
    acFac.setBrokerURL("tcp://crsvcdevlnx01:61616");
    acFac.setUserName("admin");
    acFac.setPassword("admin");
    return new CachingConnectionFactory(acFac);
    }
    }

    //Message send code
    public static void main(String args[]) throws Throwable {
    AnnotationConfigApplicationContext context =
    new AnnotationConfigApplicationContext(JmsReceiver.class);

    JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
    System.out.println("Sending a new mesage.");

    MessageCreator messageCreator = new MessageCreator() {
    @Override
    public Message createMessage(Session session) throws JMSException {
    return session.createTextMessage("ping!");
    }
    };
    jmsTemplate.send(lsamsErrorQueue, messageCreator);

    context.close();
    }
    }
  • 最佳答案

    好吧,看起来您的类路径中的版本困惑。

    首先,您不应该像使用 spring-integration-java-dsl 那样手动混合相同的工件。和 spring-integration-flow .顺便说一句,你真的需要最后一个吗?..我的意思是有什么理由保留spring-integration-flow ?这个项目是关于Modular Flows .

    从另一边你不需要指定 spring-integration-core如果您基于 Spring Boot(在您的情况下为 spring-boot-starter-integration)。

    是的:TrackableComponent已移至 org.springframework.integration.support.management从 Spring Integration 4.2 ( https://jira.spring.io/browse/INT-3799 ) 开始。

    从这里看来,您以某种方式使用了较旧的 Spring Integration 版本:
    - 或 Spring Boot 1.2.x
    - 或者它确实是来自 spring-integration-flow 的传递依赖的副作用...

    关于spring-boot - 使用 Spring boot 和集成 DSL,出现错误 ClassNotFoundException integration.history.TrackableComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34683309/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com