CompletableFuture thenCompose after exceptionally - java

I'm stuck with CompletableFuture exception handling
My logic is to send email and save status of this action. If send email throws exception I need to save the status with the exception message.
public interface MyService {
CompletableFuture<Boolean> sendEmail(String content, String address);
CompletableFuture<StatusResult> saveStatus(String content, String address);}
Processor class currently has this code. It works properly, but is not graceful as for me. How can we get rid of error local field that we use to share state between stages?
#Component
public class Processor {
private static final Logger LOGGER = LoggerFactory.getLogger(Processor.class);
#Autowired
private MyService myService;
public CompletableFuture<StatusResult> sendEmail(String content, String address) {
AtomicReference<String> error = new AtomicReference<>();// just to forward error message from exception block to thenCompose
return myService.sendEmail(content, address).exceptionally(e -> {
LOGGER.error("Exception during send email ", e);
error.set(e.getMessage());
return null;
}).thenCompose(x -> {
if (x == null) {
return myService.saveStatus(error.get(), address);
} else {
return myService.saveStatus("good", address);
}
});
}
}
Looks like handle method should help, but it returns CompletableFuture of CompletableFuture
public CompletableFuture<StatusResult> sendEmail(String content, String address) {
CompletableFuture<CompletableFuture<StatusResult>> result = myService.sendEmail(content, address).handle((x, e) -> {
if (e != null) {
LOGGER.error("Exception during send email ", e);
return myService.saveStatus("error", address);
} else {
return myService.saveStatus("good", address);
}
});
}

You can convert to your save status upfront.
public CompletableFuture<String> sendEmail(String content, String address) {
return myService.sendEmail(content, address)
.thenApply(b -> "good")
.exceptionally(Throwable::getMessage)
.thenCompose(status -> myService.saveStatus(status, address));
}

Another working solution :
public CompletableFuture<StatusResult> sendEmailAndSaveStatus(String content, String address) {
CompletableFuture<Boolean> sendEmail = myService.sendEmail(content, address);
CompletableFuture<StatusResult> result = new CompletableFuture<>();
sendEmail.exceptionally(e -> {
LOGGER.info("Exception during send email ");
myService.saveStatus(e.getMessage(), address).thenApply(x -> result.complete(x));
return false;
});
sendEmail.thenCompose(x -> myService.saveStatus("good", address)).thenApply(x -> result.complete(x));
return result;
}

Related

WebClient Spring Boot : Data is saved only if it returns value

I am using Mailchimp api when I return value it works fine but when I don't return any value it does not work and also not throwing any exception.
#GetMapping(value = "/members/add/{email}") //using GET to test
public Mono<String> addMember(#PathVariable String email) {
User user = new User();
user.setFirst_name("Test");
user.setLast_name("Data");
user.setEmail_id(email);
return mailchimpService.create(user);
}
This is not saving data
#GetMapping(value = "/members/add/{email}") //using GET to test
public Mono<String> addMember(#PathVariable String email) {
User user = new User();
user.setFirst_name("Test");
user.setLast_name("Data");
user.setPhone("");
user.setEmail_id(email);
mailchimpService.create(user);
return null;
}
public Mono<String> create(User user) {
try {
Members members = convertUserToMailchimpObject(user);
return webClient.post()
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(members))
.exchangeToMono(clientResponse -> {
if (clientResponse.statusCode().is5xxServerError()) {
clientResponse.body((clientHttpResponse, context) -> clientHttpResponse.getBody());
System.out.println(clientResponse.statusCode() + " statusCode error");
System.out.println(clientResponse.statusCode().value() + " value error");
return clientResponse.bodyToMono(String.class);
} else
System.out.println(clientResponse.statusCode() + " statusCode");
System.out.println(clientResponse.statusCode().value() + " value");
return clientResponse.bodyToMono(String.class);
});
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
The reason is that in reactive programming nothing happens until you subscribe. The create method is never get subscribed thus the webclient call is never made.
The first snippet works because the Webflux framework will subscribe for you as long as you provide your publisher(a Mono).

Request is not send without block()

I want to use this webflux client code to send POST requests with reply and without reply. I tried this code implementation:
public class RestClientBuilder {
private String token;
private String username;
private String password;
private URL gatewayUrl;
private SslContextBuilder sslContextBuilder;
public static RestClientBuilder builder() {
return new RestClientBuilder();
}
public RestClientBuilder token(String token) {
this.token = validateAndTrim(token, "Token");
return this;
}
public RestClientBuilder usernamePassword(String username, String password) {
this.username = validateAndTrim(username, "Username");
this.password = validateAndTrim(password, "Password");
return this;
}
private String validateAndTrim(String value, final String parameter) {
if (value == null || value.trim().isEmpty()) {
throw new IllegalArgumentException(parameter + " is empty");
}
return value.trim();
}
public RestClientBuilder gatewayUrl(String gatewayUrl) {
String urlSt = validateAndTrim(gatewayUrl, "Gateway URL");
try {
this.gatewayUrl = new URL(urlSt);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Malformed URL: " + urlSt, e);
}
return this;
}
public RestClientBuilder truststore(File truststoreFile) {
getSslContextBuilder().trustManager(truststoreFile);
return this;
}
public RestClientBuilder sslCertificate(File keyCertChainFile, File keyFile, String keyPassword) {
getSslContextBuilder().keyManager(keyCertChainFile, keyFile, keyPassword);
return this;
}
public RestClient build() throws SSLException {
SslContext sslContext = sslContextBuilder != null ? sslContextBuilder.build() : null;
return new RestClient(gatewayUrl.toString(), token, username, password, sslContext);
}
private SslContextBuilder getSslContextBuilder() {
if (sslContextBuilder == null) {
sslContextBuilder = SslContextBuilder.forClient();
}
return sslContextBuilder;
}
}
Implementation of the rest client:
public class RestClient {
private WebClient client;
private String gatewayUrl;
public RestClient(String gatewayUrl, String token, String username, String password, SslContext sslContext) {
this.gatewayUrl = gatewayUrl;
WebClient.Builder builder = WebClient.builder().baseUrl(gatewayUrl);
if (sslContext != null) {
HttpClient httpClient = HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext));
ClientHttpConnector httpConnector = new ReactorClientHttpConnector(httpClient);
builder.clientConnector(httpConnector);
}
if (username != null && password != null) {
builder.filter(basicAuthentication(username, password));
}
client = builder.build();
}
public Mono<Void> executeOnly(ReportRequest transaction) {
Mono<ReportRequest> transactionMono = Mono.just(transaction);
return client.post().uri(gatewayUrl)
.accept(MediaType.APPLICATION_XML)
.contentType(MediaType.APPLICATION_XML)
.body(transactionMono, ReportRequest.class)
.retrieve()
.bodyToMono(Void.class);
}
}
Make remote calls:
public class ReportingProcessor {
private String URL2 = "......";
public void collectEnvironmentData() throws JAXBException {
ReportRequest report = new ReportRequest();
report.setVersion("1.0");
RestClient client = null;
try {
client = RestClientBuilder.builder()
.gatewayUrl(URL2)
// .token(contract.getTerminal_token())
// .usernamePassword("user", "password")
// .truststore(new File("server.pem"))
// .sslCertificate(new File("client.pem"), new File("clientKey.p8"), "secret")
.build();
} catch (SSLException e) {
e.printStackTrace();
}
Mono<Void> result = client.executeOnly(report);
Void response = result.block();
}
When I remove Void response = result.block(); the request is not send. I Can't find why. Can you give me some advice how to make the client code working without using block().
Whenever you work with Spring-webflux you have to keep one thing in mind. i.e You don't have to break your chain. because it is necessary to, someone should call subscribe on your chain. as it works on RXJava specification.
if you break the chain then you have to call block() which not recommended.
you have to modify your code in the below manner.
Let's Consider you have a handler which is making a call to your collectEnvironmentData() method and your method is making a call to remote service.
public Mono<ServerResponse> handelerMethod(ServerRequest request){
return collectEnvironmentData().flatMap(aVoid -> ServerResponse.ok().build());
}
your method should be modified to
public Mono<Void> collectEnvironmentData() throws JAXBException {
ReportRequest report = new ReportRequest();
report.setVersion("1.0");
RestClient client = null;
try {
client = RestClientBuilder.builder()
.gatewayUrl(URL2)
// .token(contract.getTerminal_token())
// .usernamePassword("user", "password")
// .truststore(new File("server.pem"))
// .sslCertificate(new File("client.pem"), new File("clientKey.p8"),
//"secret").build();
} catch (SSLException e) {
e.printStackTrace();
}
return client.executeOnly(report);
}
Change your implementation in the above manner, hope it will work.
How I would implement your method is:
public Mono<Void> executeOnly(ReportRequest transaction) {
Mono<ReportRequest> transactionMono = Mono.just(transaction);
return client.post().uri(gatewayUrl)
.accept(MediaType.APPLICATION_XML)
.contentType(MediaType.APPLICATION_XML)
.body(transaction, ReportRequest.class)
.exchange()
.then();
}
And then I would use it as follows:
client.executeOnly(report).subscribe()
Change the method return type to Mono<Void> for end to end streaming.
public void collectEnvironmentData() throws JAXBException {
ReportRequest report = new ReportRequest();
report.setVersion("1.0");
RestClient client = null;
try {
client = RestClientBuilder.builder()
.gatewayUrl(URL2)
.build();
} catch (SSLException e) {
e.printStackTrace();
}
return client.executeOnly(report);
}
Or you can also subscribe the Mono
client.executeOnly(report).subscribe();

Get a message's ID using Gmail API from a list so it can be used later in automation

I currently have a quick block of code that will sort through the automation's Gmail account to find the latest message, and list its ID. How exactly can I save that ID to a separate string, so it can be used later on to get the message for comparison. Am I missing a specific line of code, or should I rewrite it in some way? Thanks.
Create a list of the messages using a query. It's going to print the ID of each message.
private List<Message> listMessage(Gmail service,
String query) throws IOException {
ListMessagesResponse response = service.users().messages().list("me").setQ(query).execute();
List<Message> messages = new ArrayList<Message>();
while (response.getMessages() != null) {
messages.addAll(response.getMessages());
if (response.getNextPageToken() != null) {
String pageToken = response.getNextPageToken();
response = service.users().messages().list("me").setQ(query)
.setPageToken(pageToken).execute();
} else {
break;
}
}
if(messages.isEmpty()) {
listMessage(service, query);
}
for (Message message : messages) { //This is going to print the ID of each message.
System.out.println(message.toPrettyString());
}
return messages;
}
This is going to find the latest one.
public void listGmailEmail() {
long unixTime = Instant.now().getEpochSecond();
try {
listMessage(service, "after: " + unixTime);
} catch (IOException ignored) { }
}
I figured it out eventually.
Get list of messages
Turn the list into a JSON
Create a method to get the message
Filter the JSON to get the message ID
Apply the message ID to the new method
Get the message
private List<Message> getMessageID(Gmail service,
String query) throws IOException {
ListMessagesResponse response = service.users().messages().list("me").setQ(query).execute();
List<Message> messages = new ArrayList<Message>();
while (response.getMessages() != null) {
messages.addAll(response.getMessages());
if (response.getNextPageToken() != null) {
String pageToken = response.getNextPageToken();
response = service.users().messages().list("me").setQ(query)
.setPageToken(pageToken).execute();
} else {
break;
}
}
if(messages.isEmpty()) {
getMessageID(service, query);
}
messageID = gson.toJson(messages);
return messages;
}
private Message getEmail(Gmail service, String userId, String messageId)
throws IOException {
Message message = service.users().messages().get(userId, messageId).execute();
email = message.toString();
return message;
}
public void getGmailEmail() {
try {
getMessageID(service, "after: " + unixTime);
messageID = messageID.split("\",")[0].substring(8);
getEmail(service,"me", messageID);
System.out.println("Email received");
emailOrThread = email;
} catch (IOException ignored) { }
}

spring - using cache

I have a spring project, and I need to use cache in my service class, like this:
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
#Service
public class WebAuthnServer {
private final Cache<String, RegistrationRequest> registerRequestStorage = newCache();
private static <K, V> Cache<K, V> newCache() {
return CacheBuilder.newBuilder()
.maximumSize(100)
.expireAfterAccess(10, TimeUnit.MINUTES)
.build();
}
public Either<String, RegistrationRequest> startRegistration(String username, String displayName, String credentialNickname, boolean requireResidentKey) {
if (userStorage.getRegistrationsByUsername(username).isEmpty()) {
RegistrationRequest request = new RegistrationRequest(...);
registerRequestStorage.put(request.getRequestId(), request);
} else {
return new Left("The username \"" + username + "\" is already registered.");
}
}
}
I have registerRequestStorage cache, and I put some data in cache using the method startRegistration. But when I try to get this data in another method, the cache is empty.
public Either<List<String>, SuccessfulRegistrationResult> finishRegistration(String responseJson) {
RegistrationResponse response = null;
try {
response = jsonMapper.readValue(responseJson, RegistrationResponse.class);
} catch (IOException e) {
return Left.apply(Arrays.asList("Registration failed!", "Failed to decode response object.", e.getMessage()));
}
RegistrationRequest request = registerRequestStorage.getIfPresent(response.getRequestId());
registerRequestStorage.invalidate(response.getRequestId());
if (request == null) {
logger.info("fail finishRegistration responseJson: {}", responseJson);
return Left.apply(Arrays.asList("Registration failed!", "No such registration in progress."));
} else {
Try<RegistrationResult> registrationTry = rp.finishRegistration(
request.getPublicKeyCredentialCreationOptions(),
response.getCredential(),
Optional.empty()
);
}
}

SubethaSmtp working example

Can you please tell me how to use SubethaSmtp library? I just want to retrieve the mails from my Gmail inbox and display them or one of them in console window.
I studied most of the API doc but I'm not being able to put the pieces together to get the things working.
Can you please tell me about a working example?
I wrote this code to build a grails application. You may find some bad code habits but it's okey for a sample application.
Here is the code in src/groovy folder :
class MessageHandlerFactoryImpl implements MessageHandlerFactory {
#Override
MessageHandler create(MessageContext ctx) {
return new MessageHandlerImpl(ctx)
}
}
class MessageHandlerImpl implements MessageHandler {
MessageContext context
MessageHandlerImpl(MessageContext context) {
this.context = context
}
#Override
void from(String from) {
println "FROM: ${from}"
}
#Override
void recipient(String recipient) {
println "RECIPIENT: ${recipient}"
}
#Override
void data(InputStream data) {
println "DATA"
println "-------------------"
BufferedReader reader = new BufferedReader(new InputStreamReader(data))
StringBuilder builder = new StringBuilder()
String line
while ((line = reader.readLine()) != null) {
builder.append(line + "\n")
}
println builder.toString()
}
#Override
void done() {
println "DONE"
}
}
class SimpleMessageListenerImpl implements SimpleMessageListener {
#Override
boolean accept(String from, String recipient) {
println "accept: ${from} \n>> ${recipient}"
return false
}
#Override
void deliver(String from, String recipient, InputStream data) {
try {
println "deliver: ${from} \n>> ${recipient} \n>>> ${data.read()}"
} catch (TooMuchDataException e) {
println "TooMuchDataException: ${e.message}"
} catch (IOException e) {
println "IOException: ${e.message}"
}
}
}
class UsernamePasswordValidatorImpl implements UsernamePasswordValidator {
#Override
void login(String username, String password) {
try {
println "LOGIN:::::::"
} catch(LoginFailedException e) {
println "LoginFailedException: ${e.message}"
}
}
}
And here is my controller code.
class SubethaController {
SMTPServer server
def index() {
MessageHandlerFactoryImpl factory = new MessageHandlerFactoryImpl()
server = new SMTPServer(factory)
server.hostName = "imap.gmail.com"
server.port = 993
server.authenticationHandlerFactory = new EasyAuthenticationHandlerFactory(new UsernamePasswordValidatorImpl())
server.start()
}
def stop() {
server?.stop()
}
Wiser wiser
def wiser() {
server = new SMTPServer(new SimpleMessageListenerAdapter(new SimpleMessageListenerImpl()))
server.start()
wiser = new Wiser()
wiser.setPort(25001)
wiser.start()
for (WiserMessage message : wiser.getMessages())
{
String eSender = message.getEnvelopeSender()
String eReceiver = message.getEnvelopeReceiver()
println ">>>>>>>message.getMimeMessage ${message.getMimeMessage()}"
}
}
def wiserS() {
wiser?.stop()
}
}
Thanks.
Okey... I found the answer... The code is well written and is working fine. I just didn't know how to send messages to listening smtp server on the port. I just used telnet program and sent emails to the smtp server running on localhost. Now I will create DNS mapping to make it work on the Internet.
Thanks Nicolás for showing your interest.

Categories

Resources