Skip to content

Commit

Permalink
INT-4540: Use MessageHeaders in GenericHandler (#2589)
Browse files Browse the repository at this point in the history
* INT-4540: Use MessageHeaders in GenericHandler

JIRA: https://jira.spring.io/browse/INT-4540

For better end-user experience use a `MessageHeaders` in the
`GenericHandler` instead of plain `Map<String, Object>`

* * Fix `IntegrationFlowEventsTests`
  • Loading branch information
artembilan authored and garyrussell committed Oct 8, 2018
1 parent 716494c commit 9ae0eb2
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,7 @@

package org.springframework.integration.handler;

import java.util.Map;
import org.springframework.messaging.MessageHeaders;

/**
* A functional interface to specify {@link org.springframework.messaging.MessageHandler}
Expand All @@ -36,6 +36,6 @@
@FunctionalInterface
public interface GenericHandler<P> {

Object handle(P payload, Map<String, Object> headers);
Object handle(P payload, MessageHeaders headers);

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import org.junit.BeforeClass;
Expand All @@ -49,6 +48,7 @@
import org.springframework.integration.store.SimpleMessageStore;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.annotation.DirtiesContext;
Expand Down Expand Up @@ -148,7 +148,7 @@ public void setFoo(Integer foo) {
}

@Override
public Object handle(Integer p, Map<String, Object> h) {
public Object handle(Integer p, MessageHeaders h) {
return p * 2;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public IntegrationFlow sseFlow() {
.requestMapping(m -> m.produces(MediaType.TEXT_EVENT_STREAM_VALUE))
.mappedResponseHeaders("*"))
.enrichHeaders(Collections.singletonMap("aHeader", new String[] { "foo", "bar", "baz" }))
.handle((p, h) -> Flux.fromArray((String[]) h.get("aHeader")))
.handle((p, h) -> Flux.fromArray(h.get("aHeader", String[].class)))
.get();
}

Expand Down
2 changes: 1 addition & 1 deletion src/reference/asciidoc/dsl.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ Similar lambda configurations are provided for the `resequence()` EIP method.
The `.handle()` EIP method's goal is to invoke any `MessageHandler` implementation or any method on some POJO.
Another option is to define an "`activity`" by using lambda expressions.
Consequently, we introduced a generic `GenericHandler<P>` functional interface.
Its `handle` method requires two arguments: `P payload` and `Map<String, Object> headers`.
Its `handle` method requires two arguments: `P payload` and `MessageHeaders headers` (starting with version 5.1).
Having that, we can define a flow as follows:

====
Expand Down
2 changes: 2 additions & 0 deletions src/reference/asciidoc/whats-new.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ This avoid confusion with `log()` which is treated as a one-way end flow compone

A generated bean name for any `NamedComponent` within an integration flow is now based on the component type for better readability from visual tools, logs analyzers and metrics collectors.

The `GenericHandler.handle()` now excepts a `MessageHeaders` type for the second argument.

[[x5.1-dispatcher-exceptions]]
==== Dispatcher Exceptions

Expand Down

0 comments on commit 9ae0eb2

Please sign in to comment.