Skip to content

Commit

Permalink
Merge branch 'observable' of https://github.com/rstoyanchev/event-sou…
Browse files Browse the repository at this point in the history
…rcing-examples into rstoyanchev-observable
  • Loading branch information
cer committed Jun 7, 2015
2 parents 7f07d53 + 6561620 commit b3ceb56
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 15 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ For more information, please see the [wiki](../../wiki)

# About the event store

The application use one of two event stores:
The application uses one of two event stores:

* embedded SQL-based event store, which is great for integration tests
* event store server
* Embedded SQL-based event store, which is great for integration tests
* Event store server

# Running the tests

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import rx.Observable;
import rx.functions.Func1;

@RestController
Expand All @@ -24,14 +26,14 @@ public AccountController(AccountService accountService) {
}

@RequestMapping(method = RequestMethod.POST)
public DeferredResult<CreateAccountResponse> createAccount(@RequestBody CreateAccountRequest request) {
return DeferredUtils.toDeferredResult(accountService.openAccount(request.getInitialBalance()).map(new Func1<EntityWithIdAndVersion<Account>, CreateAccountResponse>() {
public Observable<CreateAccountResponse> createAccount(@RequestBody CreateAccountRequest request) {
return accountService.openAccount(request.getInitialBalance()).map(new Func1<EntityWithIdAndVersion<Account>, CreateAccountResponse>() {

@Override
public CreateAccountResponse call(EntityWithIdAndVersion<Account> entityAndEventInfo) {
return new CreateAccountResponse(entityAndEventInfo.getEntityIdentifier().getId());
}

}));
});
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.accounts;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.accounts.AccountConfiguration;
import net.chrisrichardson.eventstore.javaexamples.banking.web.util.ObservableReturnValueHandler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

@Configuration
@Import({AccountConfiguration.class})
@ComponentScan
public class CommandSideWebAccountsConfiguration {
public class CommandSideWebAccountsConfiguration extends WebMvcConfigurerAdapter {

@Autowired
private RequestMappingHandlerAdapter adapter;

@PostConstruct
public void init() {
// https://jira.spring.io/browse/SPR-13083
List<HandlerMethodReturnValueHandler> handlers = new ArrayList<HandlerMethodReturnValueHandler>(adapter.getReturnValueHandlers());
handlers.add(0, new ObservableReturnValueHandler());
adapter.setReturnValueHandlers(handlers);
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
package net.chrisrichardson.eventstore.javaexamples.banking.web.queryside;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts.QuerySideAccountConfiguration;
import net.chrisrichardson.eventstore.javaexamples.banking.web.util.ObservableReturnValueHandler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

@Configuration
@Import({QuerySideAccountConfiguration.class})
@ComponentScan
public class QuerySideWebConfiguration {
public class QuerySideWebConfiguration extends WebMvcConfigurerAdapter {

@Autowired
private RequestMappingHandlerAdapter adapter;

@PostConstruct
public void init() {
// https://jira.spring.io/browse/SPR-13083
List<HandlerMethodReturnValueHandler> handlers = new ArrayList<HandlerMethodReturnValueHandler>(adapter.getReturnValueHandlers());
handlers.add(0, new ObservableReturnValueHandler());
adapter.setReturnValueHandlers(handlers);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;

import rx.Observable;
import rx.functions.Func1;

import java.math.BigDecimal;
Expand All @@ -24,13 +26,13 @@ public AccountQueryController(AccountQueryService accountInfoQueryService) {
}

@RequestMapping(value="/accounts/{accountId}", method = RequestMethod.GET)
public DeferredResult<GetAccountResponse> get(@PathVariable String accountId) {
return DeferredUtils.toDeferredResult(accountInfoQueryService.findByAccountId(new EntityIdentifier(accountId)).map(new Func1<AccountInfo, GetAccountResponse>() {
public Observable<GetAccountResponse> get(@PathVariable String accountId) {
return accountInfoQueryService.findByAccountId(new EntityIdentifier(accountId)).map(new Func1<AccountInfo, GetAccountResponse>() {
@Override
public GetAccountResponse call(AccountInfo accountInfo) {
return new GetAccountResponse(accountInfo.getId(), new BigDecimal(accountInfo.getBalance()));
}
}));
});
}

@ResponseStatus(value= HttpStatus.NOT_FOUND, reason="account not found")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.chrisrichardson.eventstore.javaexamples.banking.web.util;

import rx.Observable;

import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.context.request.async.WebAsyncUtils;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;


public class ObservableReturnValueHandler implements HandlerMethodReturnValueHandler {

@Override
public boolean supportsReturnType(MethodParameter returnType) {
return Observable.class.equals(returnType.getParameterType());
}

@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest) throws Exception {

if (returnValue == null) {
return;
}
DeferredResult<?> d = DeferredUtils.toDeferredResult((Observable<?>) returnValue);
WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(d, mavContainer);
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.transactions;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.transactions.MoneyTransferConfiguration;
import net.chrisrichardson.eventstore.javaexamples.banking.web.util.ObservableReturnValueHandler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

@Configuration
@Import({MoneyTransferConfiguration.class})
@ComponentScan
public class CommandSideWebTransactionsConfiguration {
public class CommandSideWebTransactionsConfiguration extends WebMvcConfigurerAdapter {

@Autowired
private RequestMappingHandlerAdapter adapter;

@PostConstruct
public void init() {
// https://jira.spring.io/browse/SPR-13083
List<HandlerMethodReturnValueHandler> handlers = new ArrayList<HandlerMethodReturnValueHandler>(adapter.getReturnValueHandlers());
handlers.add(0, new ObservableReturnValueHandler());
adapter.setReturnValueHandlers(handlers);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import rx.Observable;
import rx.functions.Func1;

@RestController
Expand All @@ -26,16 +28,16 @@ public MoneyTransferController(MoneyTransferService moneyTransferService) {
}

@RequestMapping(method = RequestMethod.POST)
public DeferredResult<CreateMoneyTransferResponse> createMoneyTransfer(@RequestBody CreateMoneyTransferRequest request) {
public Observable<CreateMoneyTransferResponse> createMoneyTransfer(@RequestBody CreateMoneyTransferRequest request) {
TransferDetails transferDetails = new TransferDetails(new EntityIdentifier(request.getFromAccountId()), new EntityIdentifier(request.getToAccountId()), request.getAmount());
return DeferredUtils.toDeferredResult(moneyTransferService.transferMoney(transferDetails).map(new Func1<EntityWithIdAndVersion<MoneyTransfer>, CreateMoneyTransferResponse>() {
return moneyTransferService.transferMoney(transferDetails).map(new Func1<EntityWithIdAndVersion<MoneyTransfer>, CreateMoneyTransferResponse>() {

@Override
public CreateMoneyTransferResponse call(EntityWithIdAndVersion<MoneyTransfer> entityAndEventInfo) {
return new CreateMoneyTransferResponse(entityAndEventInfo.getEntityIdentifier().getId());
}

}));
});
}

}

0 comments on commit b3ceb56

Please sign in to comment.