Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring集成TcpOutboundGateway、ServiceActivator、消息通道和错误消息DispatchingException:Dispatcher没有订阅服务器_Java_Spring_Spring Boot_Tcp_Spring Integration - Fatal编程技术网

Java Spring集成TcpOutboundGateway、ServiceActivator、消息通道和错误消息DispatchingException:Dispatcher没有订阅服务器

Java Spring集成TcpOutboundGateway、ServiceActivator、消息通道和错误消息DispatchingException:Dispatcher没有订阅服务器,java,spring,spring-boot,tcp,spring-integration,Java,Spring,Spring Boot,Tcp,Spring Integration,正如@Gary Russell在本文中提到的,我扩展了TcpOutboundGateway,以便在不发出任何请求的情况下从TCP服务器接收消息 这里是我的定制TCPOUTBOUNDGATHEWAY,如果消息负载包含“freqID”,则它会将消息发送到MessageChannel public class ExtendedTcpOutboundGateway extends TcpOutboundGateway { private final DirectChannel unso

正如@Gary Russell在本文中提到的,我扩展了TcpOutboundGateway,以便在不发出任何请求的情况下从TCP服务器接收消息

这里是我的定制TCPOUTBOUNDGATHEWAY,如果消息负载包含“freqID”,则它会将消息发送到MessageChannel

    public class ExtendedTcpOutboundGateway extends TcpOutboundGateway {

    private final DirectChannel unsolicitedMessageChannel;


    public ExtendedTcpOutboundGateway(DirectChannel unsolicitedMessageChannel) {
        this.unsolicitedMessageChannel = unsolicitedMessageChannel;
    }

    @Override
    public boolean onMessage(Message<?> message) {
        if (isUnsolicitedMessage((Message<byte[]>) message)) {
            this.messagingTemplate.send(this.unsolicitedMessageChannel, message);
            return false;
        } else {
            return super.onMessage(message);
        }
    }

    private boolean isUnsolicitedMessage(Message<byte[]> message) {
        byte[] payloadByte = message.getPayload();

        String payloadString = new String(payloadByte);

        System.out.println(payloadString);

        return payloadString.contains("freqID");
    }
}
当我使用这样的组合时,我会有一个例外,比如

org.springframework.messaging.MessageDeliveryException:Dispatcher没有通道“未经请求的MessageChannelName\u测试”的订户

我不明白为什么我要采取这个例外,因为不一样,但类似的用法是工作的罚款从这个


我想我对ServiceActivator的设计或使用方式有问题,我应该怎么做才能清除异常?您有什么建议吗?

您喜欢在
createNewSubflow()中这样做。

并且,您不会在应用程序上下文中将其注册为bean。因此,这个对象没有与前面提到的
@ServiceActivator
绑定,并且在运行时它确实没有任何订户。您甚至不需要创建该对象。你需要的是采取现有的措施 如果您确实希望将消息传递到
@ServiceActivator
,则从应用程序上下文中获取该通道的bean


考虑使用
createNewSubflow()
BeanFactory
注入组件,以调用其
getBean(未经请求的MessageChannelName,DirectChannel.class)
使用合适的service activator订户访问real bean。

我将尝试应用您的建议并提供反馈,但我记得您是spring integration的参与者之一,因此您能否告诉我组件(MessageChannel、ServiceActivator、TcpOutBoundGateway等)的设计或使用情况你说得对吗?或者,如果你让它更方便,你会有什么建议。我的主要场景就在这里。Hello@ArtemBilan,我刚刚在createNewSubflow()方法中尝试了DirectChannel DirectChannel=getBeanFactory().getBean(未经请求的MessageChannelName,DirectChannel.class),异常消失了。谢谢你的支持。
private MessageChannel createNewSubflow(Message<?> message) {
    String host = (String) message.getHeaders().get("host");
    Integer port = (Integer) message.getHeaders().get("port");
    String unsolicitedMessageChannelName= (String) message.getHeaders().get("unsolicitedMessageChannelName");

    Assert.state(host != null && port != null, "host and/or port header missing");
    String hostPort = host + port;

    TcpNetClientConnectionFactory cf = new TcpNetClientConnectionFactory(host, port);
    cf.setLeaveOpen(true);

    ByteArrayCrLfSerializer byteArrayCrLfSerializer = new ByteArrayCrLfSerializer();
    byteArrayCrLfSerializer.setMaxMessageSize(1048576);

    cf.setSerializer(byteArrayCrLfSerializer);
    cf.setDeserializer(byteArrayCrLfSerializer);

    DirectChannel directChannel = MessageChannels.direct(unsolicitedMessageChannelName).get();

    ExtendedTcpOutboundGateway tcpOutboundGateway = new ExtendedTcpOutboundGateway(directChannel);
    tcpOutboundGateway.setConnectionFactory(cf);

    IntegrationFlow flow = f -> f.handle(tcpOutboundGateway);

    IntegrationFlowContext.IntegrationFlowRegistration flowRegistration =
        this.flowContext.registration(flow)
            .addBean(cf)
            .id(hostPort + ".flow")
            .register();
    MessageChannel inputChannel = flowRegistration.getInputChannel();

    this.subFlows.put(hostPort, inputChannel);
    return inputChannel;
}
@Service
public class PeriodicalData implements PeriodicalDataService {
    public void setPeriodicalDataOrder(some parameters) {


        String unsolicitedMessageChannelName="unsolicitedMessageChannelName_Test";

        byte[] result = tcpClientGateway.send(data, ip, port ,unsolicitedMessageChannelName);
        String response = new String(result);
        System.out.println("Here response for request data : " + response +" received");

    }

    @ServiceActivator(inputChannel = "unsolicitedMessageChannelName_Test")
    public void handle(String in) {
        System.out.println("Here unsolicitedMessageChannel data : " + in+" received");
    }
}
DirectChannel directChannel = MessageChannels.direct(unsolicitedMessageChannelName).get();