I am newbie in RxJava and need help to improve my code. Here is what I've done:
public Single<List<MenuItemsBlocks>> loadMenuItemsBlocks() {
Completable.fromAction(() -> DataStoreRepository.deleteMenuItemsBlock())
.subscribeOn(Schedulers.io()).blockingAwait();
List<MenuItemsBlocks> blocks = new ArrayList<>();
Set<String> aliasList = getAliasFromMenuItems();
for (String alias : aliasList) {
List<MenuItemsBlocks> itemsBlocks = ApiRepository.getMenuItemBlocks(alias)
.subscribeOn(Schedulers.io())
.flatMapIterable(list -> list)
.map(item -> new MenuItemsBlocks(
item.getId(),
item.getType(),
item.getImagePosition(),
item.getTextField(),
item.getSortOrder(),
item.getFileTimeStamp(),
alias
))
.doOnNext(block -> DataStoreRepository.saveMenuItemsBlock(block))
.subscribeOn(Schedulers.io())
.toList()
.blockingGet();
blocks.addAll(itemsBlocks);
}
return Single.just(blocks);
}
There is no problem at runtime with this code, but I want to improve it in rx style, I've tried to rewrite it something like this (but it's not working):
public Single<List<MenuItemsBlocks>> loadMenuItemsBlocks() {
Completable.fromAction(() -> DataStoreRepository.deleteMenuItemsBlock())
.subscribeOn(Schedulers.io()).blockingAwait();
Set<String> aliasList = getAliasFromMenuItems();
return Observable.fromIterable(aliasList)
.switchMap(alias -> ApiRepository.getMenuItemBlocks(alias)
.subscribeOn(Schedulers.io())
.flatMapIterable(list -> list)
.map(item -> new MenuItemsBlocks(
item.getId(),
item.getType(),
item.getImagePosition(),
item.getTextField(),
item.getSortOrder(),
item.getFileTimeStamp(),
alias
))
.doOnNext(block -> DataStoreRepository.saveMenuItemsBlock(block))
.subscribeOn(Schedulers.io())
.toList()
);
}
And I am stuck with it and need your help!
First of all, if you have blockingAwait in non-test code, you are doing it wrong. Second, you probably need concatMap instead of switchMap as it will just keep switching to later list elements, cancelling the outstanding API calls.
public Single<List<MenuItemsBlocks>> loadMenuItemsBlocks() {
return Completable.fromAction(() -> DataStoreRepository.deleteMenuItemsBlock())
.subscribeOn(Schedulers.io())
.andThen(Single.defer(() -> {
Set<String> aliasList = getAliasFromMenuItems();
return Observable.fromIterable(aliasList)
.concatMap(alias -> ApiRepository.getMenuItemBlocks(alias)
.subscribeOn(Schedulers.io())
.flatMapIterable(list -> list)
.map(item -> new MenuItemsBlocks(
item.getId(),
item.getType(),
item.getImagePosition(),
item.getTextField(),
item.getSortOrder(),
item.getFileTimeStamp(),
alias
))
.doOnNext(block -> DataStoreRepository.saveMenuItemsBlock(block))
.subscribeOn(Schedulers.io())
)
.toList();
}));
}
Related
If it wasn't reactive I'd do something like.
/**
* Given a refresh token and the user name, create a "payload" to represent
* secret data and store it into Redis as a hash and set it to expire in 30 seconds.
*/
Map<String, String> provideAuthenticatedData(String refreshTokenMono, String username) {
var ops = redisTemplate.opsForHash();
var payload = Map.of(
"username", username,
"secret", UUID.randomUUID().toString()
);
var puts = payload.entrySet()
.stream()
.map(e->ops.putIfAbsent(refreshToken, e.key(), e.value())
.filter(success -> !success) // finds those that have failed
.toList();
if (!puts.isEmpty()) {
throw new IllegalStateException("some elements failed to save");
}
var expireCheck = redisTemplate.expireAt(refreshToken, Instant.now().plusSeconds(30));
if (!expireCheck) {
throw new IllegalStateException("unable to expire");
}
return payload;
}
Trying to do it with Reactive it looks to get a bit messier and I got stuck after a point
/**
* Given a refresh token mono and the user name, create a "payload" to represent
* secret data and store it into Redis as a hash and set it to expire in 30 seconds.
*/
Mono<Map<String, String>> provideAuthenticatedData(Mono<String> refreshTokenMono, String username) {
var ops = reactiveRedisTemplate.opsForHash();
var payload = Map.of(
"username", username,
"secret", UUID.randomUUID().toString()
);
return refreshTokenMono
.flatMapIterable(
refreshToken -> payload.entrySet()
.stream()
.map(
e -> ops.putIfAbsent(refreshToken, e.getKey(), e.getValue())
)
.toList() // can't find an operator that would take a stream
)
// at this point I have a Flux<Mono<Boolean>>
// somehow I have to find out if any of them are false then return a Mono.error()
// then once all of it is done, set the key to expire
// finally return the payload I originally created
}
Another approach I did was this but it does not do any error handling.
Mono<Map<String, String>> provideAuthenticatedDataMono(
Mono<String> refreshTokenMono, String username) {
var ops = reactiveRedisTemplate.opsForHash();
var payload = Map.of(
"username", username,
"secret", UUID.randomUUID().toString()
);
return refreshTokenMono
.doOnNext(
refreshToken ->
payload
.entrySet()
.stream()
.map(
e -> ops.putIfAbsent(
refreshToken,
e.getKey(),
e.getValue())
)
.forEach(Mono::subscribe)
)
.doOnNext(
refreshToken ->
redisTemplate
.expireAt(
refreshToken,
Instant.now().plusSeconds(30)
)
.subscribe()
)
.flatMap((x) -> just(payload));
}
}
The main idea of reactive is to work in stream so you should avoid subscribing everywhere you should just return the Flux, Mono Stream.
THe first example is not working as you dont subscribe the mono that redis gives you.
As you are using reactive why you mix it with java stream.
one solution would be like
public Mono< YOUR_OBJECT > save(Object YOUR_OBJECT) {
return template.opsForValue().set(YOUR_OBJECT.key, YOUR_OBJECT)
.filter(aBoolean -> aBoolean)
.map(aBoolean -> YOUR_OBJECT)
.switchIfEmpty(Mono.error(new RuntimeException("Could not save data to redis")));
}
And you should be continuing the stream till the end when it will be subscribed by a controller or you
I'm trying to rewrite code with nested conditions using Optional and Stream. That's how he looked:
if (transaction.getObjectByName("EDIT_EMPLOYEE_WORKSTATION") != null) {
editObj = (EmployeeWorkstation) transaction.getObjectByName("EDIT_EMPLOYEE_WORKSTATION");
} else {
if (editObj != null) {
editObj = editObj.getEditInstance(transaction);
} else {
editObj = HOME.newEmployeeWorkstation(compId);
}
}
I tried to rewrite so:
editObj =
ofNullable(
(EmployeeWorkstation) transaction.getObjectByName("EDIT_EMPLOYEE_WORKSTATION"))
.orElse(
editObj != null
? editObj.getEditInstance(transaction)
: HOME.newEmployeeWorkstation(compId));
And it works fine but my mentor said that it can be simplified
then I tried so:
editObj =
Optional.ofNullable(
(EmployeeWorkstation) transaction.getObjectByName("EDIT_EMPLOYEE_WORKSTATION"))
.map(obj -> obj.getEditInstance(transaction))
.orElse(HOME.newEmployeeWorkstation(compId));
I understand that my .map() does not work as described above in the first versions. How can I rewrite .map so that it works as described above?
You can use a nested Optional:
EmployeeWorkstation edit = Optional.ofNullable((EmployeeWorkstation) transaction.getObjectByName("EDIT_EMPLOYEE_WORKSTATION"))
.orElseGet(() -> Optional.ofNullable(editObj)
.map(e -> e.getEditInstance(transaction))
.orElseGet(() -> HOME.newEmployeeWorkstation(compId)));
If you are using Java 9 or higher you can use Optional.or():
EmployeeWorkstation edit = Optional.ofNullable((EmployeeWorkstation) transaction.getObjectByName("EDIT_EMPLOYEE_WORKSTATION"))
.or(() -> Optional.ofNullable(editObj).map(edit -> edit.getEditInstance(transaction)))
.orElseGet(() -> HOME.newEmployeeWorkstation(compId));
I want to return JSON from Rest API endpoint as keys with values. Example:
{
"terminal 1":
{"date":"2018-10-06T00:00:00.000+0000","volume":111,"count":1},
"terminal 2":
{"date":"2018-11-06T00:00:00.000+0000","volume":122,"count":1}
}
How I can add the keys? It should be I suppose like this:
List<String<List<TopTerminalsDTO>>>>
Can you give me some code example?
Latest attempt to clean the final code:
#GetMapping("/terminals")
public ResponseEntity<Map<Integer, List<TopTerminalsDTO>>> getTopTerminalsVolumes(
#RequestParam(value = "start_date", required = true) String start_date,
#RequestParam(value = "end_date", required = true) String end_date) {
LocalDateTime start_datel = LocalDateTime.now(Clock.systemUTC());
LocalDateTime end_datel = LocalDateTime.now(Clock.systemUTC());
final List<PaymentTransactionsDailyFacts> list = dashboardRepository.top_daily_transactions(start_datel, end_datel);
final Collector<PaymentTransactionsDailyFacts, List<TopTerminalsDTO>, List<TopTerminalsDTO>> terminalsCollector =
Collector.of(
ArrayList::new,
(terminals, p) -> terminals.add(mapper.toTopTerminalsDTO(p)),
(accumulator, terminals) -> {
accumulator.addAll(terminals);
return accumulator;
}
);
final Map<Integer, List<TopTerminalsDTO>> final_map =
list.stream()
.filter(p -> p.getTerminal_id() != null)
.collect(Collectors.groupingBy(p -> p.getTerminal_id(), terminalsCollector));
return ResponseEntity.ok(final_map);
}
Following your JSON, testDate() should return Map<String, TopTerminalsDTO> instead of List.
Map<String, TopTerminalsDTO> result = newHashMap();
for (int i = 0; i <= 10; i++) {
TopTerminalsDTO ttDto = new TopTerminalsDTO();
ttDto.setCount(ThreadLocalRandom.current().nextInt(20, 500 + 1));
LocalDate localDate = LocalDate.now().minus(Period.ofDays((new Random().nextInt(365 * 70))));
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
ttDto.setDate(date);
ttDto.setVolume(ThreadLocalRandom.current().nextInt(300, 5000 + 1));
result.put("terminal "+i, ttDto)
}
return result;
And, of course, change response type of rest method to ResponseEntity<Map<String, TopTerminalsDTO>>
This is what a Javascript dictionary looks like.
In Java, the correct representation is a Map<String, TopTerminalDto>.
Say you have an ordered List, and you want to return a Map with generated keys terminal{index}.
final List<TopTerminalDto> list = ...
final Map<String, TopTerminalDto> map =
IntStream.range(0, list.size())
.boxed()
.collect(toMap(i -> "terminal" + i, i -> list.get(i)));
The Spring endpoint would become:
#GetMapping("terminals")
Map<String, TopTerminalDto> getTopTerminalVolumes() { ... }
The ResponseEntity is not mandatory in Spring.
Remember to work as much as possible via Stream(s), to produce results without intermediate temporary state.
Additional example:
final List<PaymentTransactionsDailyFacts> list =
dashboardRepository.top_daily_transactions(start_datel, end_datel);
final Map<String, TopTerminalDto> map =
list.stream()
.collect(toMap(p -> p.getTerminal(), this::toDto))
// Conversion method
private TopTerminalDto toDto(final PaymentTransactionsDailyFacts p) {
// Implement conversion to Dto
}
For multiple values associated with a terminal:
final Map<Integer, List<TopTerminalDto>> map =
list.stream()
.filter(p -> p.getTerminal() != null)
.collect(groupingBy(
p -> p.getTerminal(),
Collector.of(
ArrayList::new,
(terminals, p) -> terminals.add(toDto(p)),
(accumulator, terminals) -> {
accumulator.addAll(terminals);
return accumulator;
}
)
));
You can clean the code by extracting the Collector.
final Collector<Integer, List<TopTerminalDto>, List<TopTerminalDto>> terminalsCollector =
Collector.of(
ArrayList::new,
(terminals, p) -> terminals.add(toDto(p)),
(accumulator, terminals) -> {
accumulator.addAll(terminals);
return accumulator;
}
)
final Map<Integer, List<TopTerminalDto>> map =
list.stream()
.filter(p -> p.getTerminal() != null)
.collect(groupingBy(p -> p.getTerminal(), terminalsCollector));
I have a basic SpringBoot app. using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file
I have this piece of code to compare POJOs, but the compartor seems not to work because lastDeviceEvent and firstDeviceEvent are the same object with the same ID
DeviceEvent lastDeviceEvent = null;
DeviceEvent firstDeviceEvent = null;
try {
lastDeviceEvent = deviceEvents
.stream()
.filter (o -> o.getId().equals(deviceId))
.sorted(comparing((DeviceEvent de) -> de.getId()).reversed())
.findFirst().get();
firstDeviceEvent = deviceEvents
.stream()
.filter (o -> o.getId().equals(deviceId))
.sorted(comparing((DeviceEvent de) -> de.getId()))
.findFirst().get();
LOG.info("lastDeviceEvent --> " + lastDeviceEvent.getId());
LOG.info("firstDeviceEvent -> " + firstDeviceEvent.getId());
} catch (NoSuchElementException nse) {
throw new AccessDeniedException("403 Forbidden");
}
The comparator seems correct. The problem seems to be in your filter clause, where you compare the event id to the device id
lastDeviceEvent = deviceEvents
.stream()
.filter (o -> o.getDeviceId().equals(deviceId)) // Original code used getId()
.sorted(comparing((DeviceEvent de) -> de.getId()).reversed())
.findFirst()
.get();
I have 2 URLs to fetch the data, for example: \location_1\{userid} and \location_2\{userid}. for the first i get the list of the users and then need to fetch user details by above requests. the issue is that i need to call the \location_1\{userid} and in case there is an error(exception) fetch the data from \location_2\{userid}. is it possible to make it with single rx-chain? i've tried try/catch as described here but looks catch newer calls, only onErrorResumeNext calls.
Observable<List<TestModel2>> observable = apiTest
.performTest()
.flatMapIterable(items -> items)
.flatMap(testModel -> {
try
{
return apiTest.performTest2(testModel.userId);
} catch (Exception e)
{
return apiTest.performTest3(testModel.userId);
}
}).doOnNext(testModel2 -> {Log.d("TestItemData", "doOnNext --- " + testModel2.title);})
.onErrorResumeNext(throwable ->{
Log.d("TestItemData", "onErrorResumeNext -------- ");
return Observable.empty();
})
.toList()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
Use onErrorResumeNext (as you already did a bit later in the flow):
Observable<List<TestModel2>> observable = apiTest
.performTest()
.flatMapIterable(items -> items)
.flatMap(testModel ->
apiTest.performTest2(testModel.userId)
.onErrorResumeNext(e -> apiTest.performTest3(testModel.userId)); // <----------------
)
.doOnNext(testModel2 -> {
Log.d("TestItemData", "doOnNext --- " + testModel2.title);
})
.onErrorResumeNext(throwable ->{
Log.d("TestItemData", "onErrorResumeNext -------- ");
return Observable.empty();
})
.toList()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());