Java 将集合与ReplyingKafkatTemplate一起使用时,相关ID丢失
这是我的听众Java 将集合与ReplyingKafkatTemplate一起使用时,相关ID丢失,java,spring-boot,spring-kafka,Java,Spring Boot,Spring Kafka,这是我的听众 @KafkaListener(topics = ["cartListHashes"]) @SendTo @Transactional fun listHashes(token: String): Collection<String> { // get id return doListHashes(token) } private fun doListHashes(token: String): Collection<String> {
@KafkaListener(topics = ["cartListHashes"])
@SendTo
@Transactional
fun listHashes(token: String): Collection<String> {
// get id
return doListHashes(token)
}
private fun doListHashes(token: String): Collection<String> {
val id = userService.lookupIdSync(token)
if (id == null) {
log.info("Cannot get user id with token $token")
return emptyList()
}
return cartRepo.listHashes(id).map { base32.encodeToString(it) }
}
@KafkaListener(主题=[“cartListHashes”])
@森托
@交易的
趣味列表哈希(标记:字符串):集合{
//取得身份证
返回doListHash(令牌)
}
私人趣味DoListHash(标记:String):集合{
val id=userService.lookupIdSync(令牌)
if(id==null){
log.info(“无法使用令牌$token获取用户id”)
返回空列表()
}
返回cartRepo.listHashes(id).map{base32.encodeToString(it)}
}
问题是相关ID丢失了
在reply:xxx中找不到correlationId-要使用请求/应答语义,响应服务器必须在“correlationId”头中返回correlationId
事实证明,我不能使用集合作为返回类型。 在
消息列表适配器中
:
protected void sendResponse(Object result, String topic, @Nullable Object source, boolean messageReturnType) {
if (!messageReturnType && topic == null) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("No replyTopic to handle the reply: " + result);
}
}
else if (result instanceof Message) {
this.replyTemplate.send((Message<?>) result);
}
else {
if (result instanceof Collection) {
((Collection<V>) result).forEach(v -> {
if (v instanceof Message) {
this.replyTemplate.send((Message<?>) v);
}
else {
this.replyTemplate.send(topic, v);
}
});
}
else {
sendSingleResult(result, topic, source);
}
}
}
受保护的void sendResponse(对象结果、字符串主题、@Nullable对象源、布尔messageReturnType){
如果(!messageReturnType&&topic==null){
if(this.logger.isDebugEnabled()){
this.logger.debug(“没有replyTopic来处理回复:+结果”);
}
}
else if(消息的结果实例){
this.replyTemplate.send((消息)结果);
}
否则{
if(集合的结果实例){
((收集)结果)。forEach(v->{
if(v instanceof Message){
this.replyTemplate.send((Message)v);
}
否则{
this.replyTemplate.send(主题,v);
}
});
}
否则{
sendSingleResult(结果、主题、来源);
}
}
}
收集结果将被视为单个消息
在我将返回类型修改为Array之后,它就可以工作了
@KafkaListener(topics = ["cartListHashes"])
@SendTo
@Transactional
fun listHashes(token: String): Array<String> {
// get id
return doListHashes(token)
}
private fun doListHashes(token: String): Array<String> {
val id = userService.lookupIdSync(token)
if (id == null) {
log.info("Cannot get user id with token $token")
return emptyArray()
}
return cartRepo.listHashes(id).map { base32.encodeToString(it) }.toTypedArray()
}
@KafkaListener(主题=[“cartListHashes”])
@森托
@交易的
趣味列表哈希(标记:字符串):数组{
//取得身份证
返回doListHash(令牌)
}
私有fun dolisthash(标记:String):数组{
val id=userService.lookupIdSync(令牌)
if(id==null){
log.info(“无法使用令牌$token获取用户id”)
返回空数组()
}
返回cartRepo.listHashes(id).map{base32.encodeToString(it)}.toTypedArray()
}
事实证明,我不能使用集合作为返回类型。
在消息列表适配器中
:
protected void sendResponse(Object result, String topic, @Nullable Object source, boolean messageReturnType) {
if (!messageReturnType && topic == null) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("No replyTopic to handle the reply: " + result);
}
}
else if (result instanceof Message) {
this.replyTemplate.send((Message<?>) result);
}
else {
if (result instanceof Collection) {
((Collection<V>) result).forEach(v -> {
if (v instanceof Message) {
this.replyTemplate.send((Message<?>) v);
}
else {
this.replyTemplate.send(topic, v);
}
});
}
else {
sendSingleResult(result, topic, source);
}
}
}
受保护的void sendResponse(对象结果、字符串主题、@Nullable对象源、布尔messageReturnType){
如果(!messageReturnType&&topic==null){
if(this.logger.isDebugEnabled()){
this.logger.debug(“没有replyTopic来处理回复:+结果”);
}
}
else if(消息的结果实例){
this.replyTemplate.send((消息)结果);
}
否则{
if(集合的结果实例){
((收集)结果)。forEach(v->{
if(v instanceof Message){
this.replyTemplate.send((Message)v);
}
否则{
this.replyTemplate.send(主题,v);
}
});
}
否则{
sendSingleResult(结果、主题、来源);
}
}
}
收集结果将被视为单个消息
在我将返回类型修改为Array之后,它就可以工作了
@KafkaListener(topics = ["cartListHashes"])
@SendTo
@Transactional
fun listHashes(token: String): Array<String> {
// get id
return doListHashes(token)
}
private fun doListHashes(token: String): Array<String> {
val id = userService.lookupIdSync(token)
if (id == null) {
log.info("Cannot get user id with token $token")
return emptyArray()
}
return cartRepo.listHashes(id).map { base32.encodeToString(it) }.toTypedArray()
}
@KafkaListener(主题=[“cartListHashes”])
@森托
@交易的
趣味列表哈希(标记:字符串):数组{
//取得身份证
返回doListHash(令牌)
}
私有fun dolisthash(标记:String):数组{
val id=userService.lookupIdSync(令牌)
if(id==null){
log.info(“无法使用令牌$token获取用户id”)
返回空数组()
}
返回cartRepo.listHashes(id).map{base32.encodeToString(it)}.toTypedArray()
}