Skip to content

Instantly share code, notes, and snippets.

@jbrisbin
Last active December 23, 2015 15:19
Show Gist options
  • Save jbrisbin/6655092 to your computer and use it in GitHub Desktop.
Save jbrisbin/6655092 to your computer and use it in GitHub Desktop.
Example usage of reactor-data with Spring Boot
@Controller
public class TradeController {
@Autowired
private ClientComposableRepository clients;
@RequestMapping(value = "/{clientId}", method = RequestMethod.POST)
@ResponseBody
public DeferredResult<String> trade(@PathVariable Long clientId) {
final DeferredResult<String> d = new DeferredResult<String>();
clients.save(clients.findOne(clientId)
.map(new Function<Client, Client>() {
public Client apply(Client cl) {
return cl.setTradeCount(cl.getTradeCount() + 1);
}
}))
.map(new Function<Client, String>() {
public String apply(Client cl) {
return "Hello " + cl.getName() + "! You now have " + cl.getTradeCount() + " trades.";
}
})
.consume(new Consumer<String>() {
public void accept(String s) {
d.setResult(s);
}
});
return d;
}
}
@Controller
public class TradeController {
@Autowired
private ClientComposableRepository clients;
@RequestMapping(value = "/{clientId}", method = RequestMethod.POST)
@ResponseBody
public DeferredResult<String> trade(@PathVariable Long clientId) {
final DeferredResult<String> d = new DeferredResult<>();
clients.save(clients.findOne(clientId)
.map(cl -> cl.setTradeCount(cl.getTradeCount() + 1)))
.map(cl -> "Hello " + cl.getName() + "! You now have " + cl.getTradeCount() + " trades.")
.consume(d::setResult);
return d;
}
}
@huntc
Copy link

huntc commented Sep 22, 2013

Indent issue? Also may be use constructor injection to minimise mutability easier to test too. :-)

How about a Scala sample? :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment