Я хочу использовать интеграцию с весной для создания простого TCP-сервера, который обменивается данными с помощью XML-сообщений. Я пытаюсь использовать TcpInboundGateway с соответствующей настройкой сортировки на входных и выходных каналах, как показано ниже.Как настроить XML-маршаллинг для сервера TCP в Spring интеграции?
@Bean
TcpNetServerConnectionFactory cf() {
TcpNetServerConnectionFactory tcf = new TcpNetServerConnectionFactory(7017);
tcf.setSerializer(new ByteArrayLfSerializer());
return tcf;
}
@Bean
TcpInboundGateway tcpGate() {
TcpInboundGateway gateway = new TcpInboundGateway();
gateway.setConnectionFactory(cf());
gateway.setRequestChannel(requestChannel());
gateway.setReplyChannel(replyChannel());
return gateway;
}
@Bean
public MessageChannel requestChannel() {
return new DirectChannel();
}
@Bean
public MessageChannel replyChannel() {
return new DirectChannel();
}
@Bean
Jaxb2Marshaller marshaller()
{
final Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan("uk.ac.man.jb.emerlin.emms.corrcontrol.messages");
return marshaller;
}
@Bean
@org.springframework.integration.annotation.Transformer(inputChannel="requestChannel", outputChannel="requestChannel")
public Transformer unmarshallingTransformer()
{
final UnmarshallingTransformer unmarshallingTransformer = new UnmarshallingTransformer(marshaller());
unmarshallingTransformer.setSourceFactory(msourceFactory());
return unmarshallingTransformer;
}
@Bean
@org.springframework.integration.annotation.Transformer(inputChannel="replyChannel", outputChannel="replyChannel")
public Transformer marshallingTransformer() throws ParserConfigurationException
{
final MarshallingTransformer marshallingTransformer = new MarshallingTransformer(marshaller());
marshallingTransformer.setResultType(AbstractXmlTransformer.STRING_RESULT);
return marshallingTransformer;
}
@Bean
public SourceFactory msourceFactory() {
return new SourceFactory() {
@Override
public Source createSource(Object payload) {
Source source = null;
if (payload instanceof String) {
source = new StreamSource(new StringReader((String) payload));
}
else if (payload instanceof byte[]) {
source = new StreamSource(new StringReader(new String((byte[])payload)));
}
if (source == null) {
throw new MessagingException("failed to create Source for payload type [" +
payload.getClass().getName() + "]");
}
return source;
}
};
}
//FIXME
@MessageEndpoint
public static class CorrelatorEmControl {
@ServiceActivator(inputChannel = "requestChannel" , outputChannel="replyChannel")
public CorrelatorResponse service(@Payload CorrelatorRequest in) {
logger.info("command {}", in.getCommand().getName());
return new CorrelatorResponse();
}
}
Моя проблема заключается в том, что несмотря на том входящем сообщении unmarshalled правильно, я не могу показаться, чтобы получить сортировочный ответ в соответствующую форму - например, строка для канала ответа, чтобы передать ее - как в следующем следе.
14:20:37.277 [pool-3-thread-2] INFO u.a.m.j.e.e.c.d.CorrelatorEndpoint - command silly
14:20:37.277 [pool-3-thread-2] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'replyChannel'
14:20:37.277 [pool-3-thread-2] DEBUG o.s.i.t.MessageTransformingHandler - correlatorEndpoint.marshallingTransformer.transformer.handler received message: GenericMessage [payload=uk[email protected]74056889, headers={timestamp=1429881637277, id=d745c519-ddd3-a92c-260f-22e91fdcc5d1, errorChannel=org.springframewor[email protected]110ed52e, ip_tcp_remotePort=55208, ip_address=0:0:0:0:0:0:0:1, replyChannel=org.springframewor[email protected]110ed52e, ip_hostname=localhost, ip_connectionId=localhost:55208:7017:8d710c70-ed34-4b3a-bdb6-d3ae9ce7142c}]
14:20:37.292 [pool-3-thread-2] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'replyChannel'
14:20:37.292 [pool-3-thread-2] DEBUG o.s.i.handler.BridgeHandler - [email protected] received message: GenericMessage [payload=<?xml version="1.0" encoding="UTF-8" standalone="yes"?><emcc:correlatorResponse xmlns:emcc="http://jb.man.ac.uk/schema/emerlincorrelator" xmlns:eop="http://jb.man.ac.uk/schema/eop"><errorStatus>false</errorStatus></emcc:correlatorResponse>, headers={timestamp=1429881637292, id=20e4eaf1-f6d0-de23-c14b-1dde7df5939d, errorChannel=org.springframewor[email protected]110ed52e, ip_address=0:0:0:0:0:0:0:1, ip_tcp_remotePort=55208, replyChannel=org.springframewor[email protected]110ed52e, ip_hostname=localhost, ip_connectionId=localhost:55208:7017:8d710c70-ed34-4b3a-bdb6-d3ae9ce7142c}]
14:20:37.293 [pool-3-thread-2] DEBUG o.s.i.channel.DirectChannel - postSend (sent=true) on channel 'replyChannel', message: GenericMessage [payload=<?xml version="1.0" encoding="UTF-8" standalone="yes"?><emcc:correlatorResponse xmlns:emcc="http://jb.man.ac.uk/schema/emerlincorrelator" xmlns:eop="http://jb.man.ac.uk/schema/eop"><errorStatus>false</errorStatus></emcc:correlatorResponse>, headers={timestamp=1429881637292, id=20e4eaf1-f6d0-de23-c14b-1dde7df5939d, errorChannel=org.springframewor[email protected]110ed52e, ip_address=0:0:0:0:0:0:0:1, ip_tcp_remotePort=55208, replyChannel=org.springframewor[email protected]110ed52e, ip_hostname=localhost, ip_connectionId=localhost:55208:7017:8d710c70-ed34-4b3a-bdb6-d3ae9ce7142c}]
14:20:37.293 [pool-3-thread-2] DEBUG o.s.i.channel.DirectChannel - postSend (sent=true) on channel 'replyChannel', message: GenericMessage [payload=uk[email protected]74056889, headers={timestamp=1429881637277, id=d745c519-ddd3-a92c-260f-22e91fdcc5d1, errorChannel=org.springframewor[email protected]110ed52e, ip_tcp_remotePort=55208, ip_address=0:0:0:0:0:0:0:1, replyChannel=org.springframewor[email protected]110ed52e, ip_hostname=localhost, ip_connectionId=localhost:55208:7017:8d710c70-ed34-4b3a-bdb6-d3ae9ce7142c}]
14:20:37.293 [pool-3-thread-2] DEBUG o.s.i.channel.DirectChannel - postSend (sent=true) on channel 'requestChannel', message: GenericMessage [payload=u[email protected]44391778, headers={timestamp=1429881637275, id=862128b7-9754-124b-e6a2-407f87d0407c, errorChannel=org.springframewor[email protected]110ed52e, ip_address=0:0:0:0:0:0:0:1, ip_tcp_remotePort=55208, replyChannel=org.springframewor[email protected]110ed52e, ip_hostname=localhost, ip_connectionId=localhost:55208:7017:8d710c70-ed34-4b3a-bdb6-d3ae9ce7142c}]
14:20:37.293 [pool-3-thread-2] DEBUG o.s.i.channel.DirectChannel - postSend (sent=true) on channel 'requestChannel', message: GenericMessage [payload=byte[254], headers={timestamp=1429881637257, id=963a80b6-ac0f-35db-51d1-a52593f9b78c, errorChannel=org.springframewor[email protected]110ed52e, ip_tcp_remotePort=55208, ip_address=0:0:0:0:0:0:0:1, replyChannel=org.springframewor[email protected]110ed52e, ip_hostname=localhost, ip_connectionId=localhost:55208:7017:8d710c70-ed34-4b3a-bdb6-d3ae9ce7142c}]
14:20:37.294 [pool-3-thread-2] DEBUG o.s.i.i.t.s.ByteArrayCrLfSerializer - Available to read:0
14:20:37.293 o.s.i.ip.tcp.TcpInboundGateway - Failed to send reply
org.springframework.messaging.MessageHandlingException: When using a byte array serializer, the socket mapper expects either a byte array or String payload, but received: class org.springframework.xml.transform.StringResult
Как настроить трансформатор для получения соответствующего вывода - StringResult, кажется, не следует интерпретировать правильно?
Спасибо, что сработало - не может повыситься, хотя у меня недостаточно репутации! –
Вы должны иметь возможность отмечать ответ как принятый, однако - это поможет другим (нажмите на контрольную отметку ниже кнопок голосования). –