I have this code and I want to know if this is possible with RxJava:
Function queries for a User from a server (async)
The server returns a User JSON object with a list of ID's of associated UserProfile(s)
Then for each of this ID, it needs to fetch the UserProfile given the ID (async also)
For each asynchronously fetched UserProfile append it to the User object, below is my pseudo-code.
I cannot use any blocking code, all request should be async.
Here's the code:
#Override
public Single<User> retrieve(String entityId) {
BaasUser baasUser = new BaasUser();
baasUser.setEntityId(entityId);
baasUser.setIncludes(Arrays.asList("userProfile"));
return baasUser.retrieve().map(user -> {
String username = user.getUsername();
String dateCreated = user.getDateCreated();
String dateUpdated = user.getDateUpdated();
List<UserProfile> userProfiles = new LinkedList<>();
BaasLink userProfileLink = user.getFirstLink();
userProfileLink.getEntities().forEach(stubEntity -> {
Single<UserProfile> observable = stubEntity.retrieve().map(entity -> {
UserProfile userProfile = new UserProfile();
userProfile.setEntityId(entity.getStringProperty("entityId"));
userProfile.setName(entity.getStringProperty("name"));
return userProfile;
});
observable.subscribe(userProfile -> {
// until all UserProfile is fetched this 'retrieve' "callback" should not return
userProfiles.add(userProfile);
}, error -> {
// err
});
});
User user1 = new User();
user1.setDateCreated(dateCreated);
user1.setDateUpdated(dateUpdated);
user1.setUsername(username);
user1.setUserProfiles(userProfiles);
return user1;
});
}
Here you have en example how to do your jobe maybe there is any typeerror becouse i dont have your objects
Single.just("user")
.observeOn(Schedulers.io())
.flatMap(user -> Observable.zip(getCurrentUserData(user),getUserProfiles(user),(t1, t2) -> {
//first func will return user eith some data next function wll return List<UserProfile> userProfiles
return newuser;
}))
.subscribeOn(Schedulers.io())
}
private Single<List<UserProfile>> getUserProfiles(User user) {
Observable.fromIterable( user.getFirstLink().getEntities())
.flatMap(stubEntity ->stubEntity.retrieve())
.map(o -> {
//set user profile data
return userprofile
})
.toList();
}
private Single<User> getCurrentUserData(User user) {
Observable.just(user)
.map(s -> {
//set data
return user;
})
}
Related
I have the following code where I call external APIs via webclient and return Mono.
I need to execute some logic when I receive data. And after all, requests are processed, execute one logic for all gathered data. I can collect all Monos and put them to flux and then execute some logic at the end. But I have serviceName filed which is accessible only in the loop, so I need to execute logic for mono in loop and here I'm stuck and don't know how to wait for all data to complete and do it in a reactive way.
#Scheduled(fixedDelay = 50000)
public void refreshSwaggerConfigurations() {
log.debug("Starting Service Definition Context refresh");
List<SwaggerServiceData> allServicesApi = new ArrayList<>();
swaggerProperties.getUrls().forEach((serviceName, serviceSwaggerUrl) -> {
log.debug("Attempting service definition refresh for Service : {} ", serviceName);
Mono<SwaggerServiceData> swaggerData = getSwaggerDefinitionForAPI(serviceName,
serviceSwaggerUrl);
swaggerData.subscribe(swaggerServiceData -> {
if (swaggerServiceData != null) {
allServicesApi.add(swaggerServiceData);
String content = getJSON(swaggerServiceData);
definitionContext.addServiceDefinition(serviceName, content);
} else {
log.error("Skipping service id : {} Error : Could not get Swagger definition from API ",
serviceName);
}
});
});
//I need to wait here for all monos to complete and after that proceed for All gathered data...
//Now it's empty And I know why, just don't know how to make it.
Optional<SwaggerServiceData> swaggerAllServicesData = getAllServicesApiSwagger(allServicesApi);
if (swaggerAllServicesData.isPresent()) {
String allApiContent = getJSON(swaggerAllServicesData.get());
definitionContext.addServiceDefinition("All", allApiContent);
}
}
private Mono<SwaggerServiceData> getSwaggerDefinitionForAPI(String serviceName, String url) {
log.debug("Accessing the SwaggerDefinition JSON for Service : {} : URL : {} ", serviceName,
url);
Mono<SwaggerServiceData> swaggerServiceDataMono = webClient.get()
.uri(url)
.exchangeToMono(clientResponse -> clientResponse.bodyToMono(SwaggerServiceData.class));
return swaggerServiceDataMono;
}
I would add a temporary class to group data and serivce name :
record SwaggerService(SwaggerServiceData swaggerServiceData, String serviceName) {
boolean hasData() {
return swaggerServiceData != null;
}
}
And then change your pipeline :
Flux.fromStream(swaggerProperties.getUrls().entrySet().stream())
.flatMap((e) -> {
Mono<SwaggerServiceData> swaggerDefinitionForAPI = getSwaggerDefinitionForAPI(e.getKey(),
e.getValue());
return swaggerDefinitionForAPI.map(swaggerServiceData -> new SwaggerService(swaggerServiceData, e.getKey()));
})
.filter(SwaggerService::hasData)
.map(swaggerService -> {
String content = getJSON(swaggerService.swaggerServiceData());
definitionContext.addServiceDefinition(swaggerService.serviceName(), content);
return swaggerService.swaggerServiceData();
})
// here we will collect all datas and they will be emmited as single Mono with list of SwaggerServiceData
.collectList()
.map(this::getAllServicesApiSwagger)
.filter(Optional::isPresent)
.map(Optional::get)
.subscribe(e -> {
String allApiContent = getJSON(e);
definitionContext.addServiceDefinition("All", allApiContent);
});
This does not deal with logging error when SwaggerServiceData is null but you can further change it if you want. Also I assume that DefinitionContext is thread safe.
Solution with error logging (using flatMap and Mono.empty()) :
Flux.fromStream(swaggerProperties.getUrls().entrySet().stream())
.flatMap((e) -> {
Mono<SwaggerServiceData> swaggerDefinitionForAPI = getSwaggerDefinitionForAPI(e.getKey(),
e.getValue());
return swaggerDefinitionForAPI
.flatMap(swaggerServiceData -> {
if(swaggerServiceData != null) {
return Mono.just(new SwaggerService(swaggerServiceData, e.getKey()));
} else {
log.error("Skipping service id : {} Error : Could not get Swagger definition from API ",
e.getKey());
return Mono.empty();
}
});
})
.map(swaggerService -> {
String content = getJSON(swaggerService.swaggerServiceData());
definitionContext.addServiceDefinition(swaggerService.serviceName(), content);
return swaggerService.swaggerServiceData();
}).collectList()
.map(this::getAllServicesApiSwagger)
.filter(Optional::isPresent)
.map(Optional::get)
.subscribe(e -> {
String allApiContent = getJSON(e);
definitionContext.addServiceDefinition("All", allApiContent);
});
You can also wrap those lambads into some meaningful methods to improve readibility.
I am using JPA to store data and faced two problems during implementation. I have two entities (Station and Commodity) that have many-to-many relationship with intermediate table so that I had to created the third one. When app receives message it converts its data to entites and should save but sometimes app throwing a ConstraintViolationException because there is null value at foreign key field referencing to Commodity entity.
I've tried simple approach: selecting needed commodity from database and saving it if there is no one. Then I started to use bulk searching all commodities of message and then putting it where are needed. None of them did a trick.
In my opinion the problem could be caused by multi-threading read\insert.
The second problem is that service stop running when exception is thrown. App can lost some of transactions that's not a big deal but it simply stops after rollback.
How can I resolve these conflicts?
Here is code of data handling class and diagram of entities :
#Service
#AllArgsConstructor
#Slf4j
public class ZeromqCommoditiesServiceImpl implements ZeromqCommoditesService {
private final CategoryTransactionHandler categoryHandler;
private final CommodityTransactionHandler commodityHandler;
private final EconomyTransactionHandler economyHandler;
private final StationTransactionHandler stationHandler;
private final SystemTransactionHandler systemHandler;
#Override
#Transactional(
isolation = Isolation.READ_COMMITTED,
propagation = Propagation.REQUIRES_NEW,
rollbackFor = Throwable.class)
#Modifying
public void saveData(ZeromqCommodityPayload payload) {
CommodityContent content = payload.getContent();
var station = stationHandler.createOrFindStation(content.getStationName());
var system = systemHandler.createOrFindSystem(content.getSystemName());
var commodityReferences = getMapOfCommodities(content);
station.setSystem(system);
updateEconomies(station, content);
updateProhibited(station, content, commodityReferences);
updateStationCommodities(station, content, commodityReferences);
try {
saveStation(station);
} catch (ConstraintViolationException | PersistentObjectException | DataAccessException e) {
log.error("Error saving commodity info \n" + content, e);
}
}
public void saveStation(StationEntity station) {
stationHandler.saveStation(station);
if (station.getId() != null) {
log.debug(String.format("Updated \"%s\" station info", station.getName()));
} else {
log.debug(String.format("Updated \"%s\" station info", station.getName()));
}
}
private void updateEconomies(StationEntity station, CommodityContent content) {
station.getEconomies().clear();
if (content.getEconomies() != null) {
var economies = content.getEconomies()
.stream()
.map(economy -> {
var stationEconomyEntity = economyHandler.createOrFindEconomy(economy.getName());
Double proportion = economy.getProportion();
stationEconomyEntity.setProportion(proportion != null ? proportion : 1.0);
return stationEconomyEntity;
})
.peek(economy -> economy.setStation(station))
.toList();
station.getEconomies().addAll(economies);
}
}
private void updateProhibited(
StationEntity station,
CommodityContent content,
Map<String, CommodityEntity> commodityEntityMap) {
station.getProhibited().clear();
if (content.getProhibited() != null) {
var prohibitedCommodityEntities = content.getProhibited()
.stream()
.map(prohibited -> {
String eddnName = prohibited.toLowerCase(Locale.ROOT);
CommodityEntity commodityReference = getCommodityEntity(commodityEntityMap, eddnName);
return new ProhibitedCommodityEntity(station, commodityReference);
}
)
.toList();
station.getProhibited().addAll(prohibitedCommodityEntities);
}
}
private void updateStationCommodities(
StationEntity station,
CommodityContent content,
Map<String, CommodityEntity> commodityEntityMap) {
station.getCommodities().clear();
if (content.getCommodities() != null) {
var commodities = content.getCommodities()
.stream()
.map(commodity -> {
CommodityEntity commodityReference = getCommodityEntity(
commodityEntityMap,
commodity.getEddnName());
return StationCommodityEntity.builder()
.commodity(commodityReference)
.buyPrice(commodity.getBuyPrice())
.sellPrice(commodity.getSellPrice())
.demand(commodity.getDemand())
.stock(commodity.getStock())
.station(station)
.build();
})
.toList();
station.getCommodities().addAll(commodities);
}
}
private CommodityEntity getCommodityEntity(Map<String, CommodityEntity> commodityEntityMap, String eddnName) {
return commodityEntityMap.get(eddnName);
}
private Map<String, CommodityEntity> getMapOfCommodities(#NotNull CommodityContent content) {
Set<String> commodities = content.getCommodities()
.stream()
.map(Commodity::getEddnName)
.collect(Collectors.toSet());
if (content.getProhibited() != null && content.getProhibited().size() > 0) {
commodities.addAll(content.getProhibited().
stream()
.map(item -> item.toLowerCase(Locale.ROOT))
.collect(Collectors.toSet()));
}
var commodityReferencesMap = commodityHandler.findAllByEddnName(commodities)
.stream()
.collect(Collectors.toMap(
CommodityEntity::getEddnName,
item -> item
));
commodities.forEach(commodity -> {
if (commodityReferencesMap.get(commodity.toLowerCase()) == null) {
CommodityCategoryEntity category = categoryHandler.createOrFindCategory("Unknown");
CommodityEntity newCommodity = new CommodityEntity(commodity, commodity, category);
CommodityEntity managedCommodity = commodityHandler.saveCommodity(newCommodity);
commodityReferencesMap.put(managedCommodity.getEddnName(), managedCommodity);
}
});
return commodityReferencesMap;
}
}
Thanks in advance
I have this function inside a #Service in Spring Webflux and it is called with a list of friends to know if everyone has been joined to a group. If some friends have no group this method calls an API to get its user's information and then calls another API to tag these guys with joined tag false.
#Service
...
public Flux<Boolean> checkUserHaveGroup(final List<String> friends) {
MatchOperation match1 = Aggregation.match(Criteria.where("friends").in(friends).and("status").is("ACTIVE"));
UnwindOperation unwind1 = Aggregation.unwind("friends");
MatchOperation match2 = Aggregation.match(Criteria.where("friends").in(friends));
GroupOperation group1 = Aggregation.group("friends");
TypedAggregation<Group> a = Aggregation.newAggregation(
Group.class,
match1, unwind1, match2, group1);
return this.reactiveMongoTemplate.aggregate(a, FriendInGroup.class)
.map(friendInGroup -> friendInGroup.id)
.collectList()
.map(users -> haveNoGroupsList(users, friends))
.flatMapMany(noGroupUsers -> {
return Flux.fromIterable(noGroupUsers)
.flatMap(pn -> crmService.deleteAttribute(pn, "joinedAGroup"));
});
}
(this method get the user information)
...
public Mono<UserInfo> userInfoById(final String userId) {
return webClient.get()
.uri(uriBuilder -> uriBuilder.path(constants.getByIdPath() + "/{id}")
.build(userId))
.header("auth", tokenService.token())
.exchange()
.flatMap(response -> {
Mono<UserInfo> responseMono;
if (response.statusCode().equals(HttpStatus.UNAUTHORIZED)) {
responseMono = Mono.error(new UnauthorizedException());
} else if (response.statusCode().equals(HttpStatus.OK)) {
responseMono = response.bodyToMono(UserInfoResponse.class)
.flatMap(uir -> Mono.just(uir.getData()));
} else {
responseMono = Mono.error(new UnhandledException());
}
return responseMono;
});
}
...
private Mono<UserInfo> getUserInfo(String userId) {
return userInfoAdapter.userInfoById(userId);
}
...
public Mono<Boolean> deleteAttribute(final String userId, final String attribute) {
return getUserInfo(userId) <<<<< here we get the users info
.flatMap(ui -> crmDeleteAttribute(ui, attribute)); <<<< this call is never done.
}
...
public Mono<Boolean> crmDeleteAttribute(final UserInfo user, final String attribute) {
return webClient.delete()
.uri(uriBuilder -> uriBuilder
.path(contants.path())
.build(user.getId(), attribute))
.header("auth", tokenService.token())
.exchange().flatMap(response -> {
if (response.statusCode().equals(HttpStatus.OK)) {
return Mono.just(Boolean.TRUE);
}
if (response.statusCode().equals(HttpStatus.BAD_REQUEST)) {
return Mono.error(CrmServiceBadRequestException::new);
}
if (response.statusCode().equals(HttpStatus.UNAUTHORIZED)) {
return Mono.error(CrmServiceUnauthorizedException::new);
}
return Mono.error(CrmServiceUnhandledException::new);
});
}
After getting the users' info on the API, the API for assign a tag is never been called no matter what I do. I can see in the debugger terminal that the call to UserInfo API was done but after that, the application returns to the controller. Someone could point me to what I'm doing wrong?
Any help is welcome.
Thanks!
I'm trying to setup a prototype for using graphql across multiple java microservices, which requires me to join multiple graphql schema's into one.
I'm using 2 java-services and the ApolloServer with ApolloGateway; which shows the following schema in the playground:
type Client {
id: ID!
name: String
linkeduser: User
}
type Query {
user(id: ID!): User
users: [User]
client(id: ID!): Client
clients: [Client]
}
type User {
id: ID!
name: String
}
When running the simple query:
query client {
client(id: 1) {
id
name
linkeduser {
id
name
}
}
}
What I expect this to return is a client with a linkeduser; When debugging the client service gets queried, the user service gets queried, yet the response is:
{
"data": {
"client": {
"id": "1",
"name": "Bob",
"linkeduser": null
}
}
}
How do I get a linked user response in my client?
I've tried returning lists of users, a new client object with a list of linkedusers, a single user.
The example of https://github.com/apollographql/federation-jvm is the base of this code, though I've yet to see this working.
Code:
Service 1: Client
#WebServlet(loadOnStartup = 1, urlPatterns = "/graphql")
public class GraphQLService extends GraphQLHttpServlet {
#Override
protected GraphQLConfiguration getConfiguration() {
return GraphQLConfiguration.with(getGraphQLSchema()).build();
}
private static GraphQLSchema getGraphQLSchema() {
InputStream inputStream = client.GraphQLService.class
.getClassLoader().getResourceAsStream("schema.graphqls");
TypeDefinitionRegistry parse = new SchemaParser().parse(inputStream);
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
.type("Query", builder -> builder.defaultDataFetcher(GraphQLService::getClient))
.build();
return com.apollographql.federation.graphqljava.Federation.transform(parse, runtimeWiring)
.fetchEntities(env -> env.<List<Map<String, Object>>>getArgument(_Entity.argumentName)
.stream()
.map(values -> {
if ("Client".equals(values.get("__typename"))) {
final Object id = values.get("id");
if (id instanceof String) {
return getSingleClient((String) id);
}
}
return null;
})
.collect(Collectors.toList()))
.resolveEntityType(env -> {
final Object src = env.getObject();
if (src instanceof Client) {
return env.getSchema().getObjectType("Client");
}
return null;
}).build();
}
private static Object getClient(DataFetchingEnvironment environment) {
switch (environment.getFieldDefinition().getName()) {
case "client":
return getSingleClient(environment.getArgument("id"));
case "clients":
return getAllClients();
default:
return null;
}
}
//... extra code with simple getters
}
With this schema :
extend type Query {
client(id: ID!): Client
clients: [Client]
}
type Client #key(fields: "id"){
id: ID!
name: String
}
Service 2: User
#WebServlet(loadOnStartup = 1, urlPatterns = "/graphql")
public class GraphQLService extends GraphQLHttpServlet {
#Override
protected GraphQLConfiguration getConfiguration() {
return GraphQLConfiguration.with(getGraphQLSchema()).build();
}
private static GraphQLSchema getGraphQLSchema() {
InputStream inputStream = user.GraphQLService.class
.getClassLoader().getResourceAsStream("schema.graphqls");
TypeDefinitionRegistry parse = new SchemaParser().parse(inputStream);
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
.type("Query", builder -> builder.defaultDataFetcher(GraphQLService::getUser))
.build();
return com.apollographql.federation.graphqljava.Federation.transform(parse, runtimeWiring)
.fetchEntities(env -> env.<List<Map<String, Object>>>getArgument(_Entity.argumentName)
.stream()
.map(values -> {
if ("Client".equals(values.get("__typename"))) {
final Object id = values.get("id");
if (id instanceof String) {
return getSingleUser((String) id);
}
}
return null;
})
.collect(Collectors.toList()))
.resolveEntityType(env -> {
final Object src = env.getObject();
if (src instanceof User) {
return env.getSchema().getObjectType("User");
}
return null;
})
.build();
}
private static Object getUser(DataFetchingEnvironment environment) {
switch (environment.getFieldDefinition().getName()) {
case "user":
return getSingleUser(environment.getArgument("id"));
case "users":
return getAllUsers();
default:
return null;
}
}
//... extra code with simple getters
}
With this schema :
type Query #extends{
user (id: ID!): User
users: [User]
}
type User #key(fields: "id") {
id: ID!
name: String
}
type Client #key(fields: "id") #extends{
id: ID! #external
linkeduser : User
}
Version in POM.xml
<graphql.version>14.0</graphql.version>
<graphql-tools.version>5.2.4</graphql-tools.version>
<graphql-servlet.version>9.0.1</graphql-servlet.version>
<graphql-federation-support.version>0.4.0</graphql-federation-support.version>
In user service, you need to return a pojo of the type client, with a getter for a linkeduser (only the extends fields need to be present):
if ("Client".equals(values.get("__typename"))) {
final Object id = values.get("id");
if (id instanceof String) {
return new Client((String) id, getSingleUser((String) id));
}
}
Also the resolveTypeEntity needs to resolve to said client
I'm relatively new to webflux and i want to find solution to avoid nested flatMap when there are conditionals:
I have 3 simple entities:
Item, Brand, Category.
Item basically contains: brandId and categoryId
public Mono<Item> patch(String itemId, PatchSpecs specs) {
return itemRepository.findById(itemId)
.switchIfEmpty(Mono.error(..)))
.flatMap(item -> {
final String brandId = specs.getBrandId();
if (brandId != null) {
return brandService.getById(brandId)
.switchIfEmpty(Mono.error(..)))
.flatMap(brand -> {
final String categoryId = specs.getCategoryId();
if (categoryId != null) {
return categoryService.getById(categoryId)... -> return updateAndSave(..)
}
return updateAndSave(specs, item, brand, null);
});
}
else {
final String categoryId = specs.getCategoryId();
if (categoryId != null) {
return categoryService.getById(categoryId)... -> return updateAndSave(..)
}
return updateAndSave(specs, item, null, null);
}
});
}
How do I prevent this branching conditional flatMaps mess? I cannot imagine if i have another entity inside Item. There will be more nested flatMaps?
If I understand your code well category and brand are optional attributes of the item. In this case I'd recommend the following:
public Mono<Item> patch(String itemId, PatchSpecs specs)
{
return itemRepository.findById(itemId)
.switchIfEmpty(Mono.error(new RuntimeException("Can not find item.")))
.flatMap(item -> updateAndSave(specs, item));
}
private Mono<? extends Item> updateAndSave(PatchSpecs specs, Item item)
{
Mono<Optional<Brand>> brand = getBrand(specs.getBrandId());
Mono<Optional<Category>> category = getCategory(specs.getCategoryId());
return Mono.zip(Mono.just(item), brand, category)
.flatMap(tuple -> updateAndSave(specs, tuple));
}
private Mono<Optional<Brand>> getBrand(String inputBrandId)
{
return Mono.justOrEmpty(inputBrandId)
.flatMap(brandId -> brandService.getById(brandId)
.switchIfEmpty(Mono.error(new RuntimeException("Can not find brand."))))
.map(Optional::of)
.defaultIfEmpty(Optional.empty());
}
private Mono<Optional<Category>> getCategory(String inputCategoryId)
{
return Mono.justOrEmpty(inputCategoryId)
.flatMap(brandId -> categoryService.getById(brandId)
.switchIfEmpty(Mono.error(new RuntimeException("Can not find brand."))))
.map(Optional::of)
.defaultIfEmpty(Optional.empty());
}
private Mono<Item> updateAndSave(PatchSpecs specs, Tuple3<Item, Optional<Brand>, Optional<Category>> tuple)
{
Item item = tuple.getT1();
Brand brand = tuple.getT2().orElse(null);
Category category = tuple.getT3().orElse(null);
// TODO do update and save here
return null;
}
It will be more extensible this way and no need for duplication and nested conditionals. Please, verify it works as you expect.