Skip to content

Instantly share code, notes, and snippets.

View viveknaskar's full-sized avatar
🎯
Focusing

Vivek Naskar viveknaskar

🎯
Focusing
View GitHub Profile
@viveknaskar
viveknaskar / AlertController.java
Created October 11, 2022 17:08
Alert Controller for sending SMS
import com.viveknaskar.smsalertsystem.domain.AlertMessage;
import com.viveknaskar.smsalertsystem.domain.User;
import com.viveknaskar.smsalertsystem.service.AlertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@viveknaskar
viveknaskar / AlertServiceImpl.java
Created October 11, 2022 17:05
Implementation of sending SMS using Twilio methods
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;
import com.viveknaskar.smsalertsystem.domain.AlertMessage;
import com.viveknaskar.smsalertsystem.domain.User;
import com.viveknaskar.smsalertsystem.service.AlertService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class AlertServiceImpl implements AlertService {
@viveknaskar
viveknaskar / application.properties
Created October 11, 2022 16:44
Application properties having credentials for accessing Twilio account
#Twilio platform credentials
twilio.account.sid=<your-twilio-account-sid>
twilio.auth.token=<your-twilio-auth-token>
@viveknaskar
viveknaskar / TwilioConfig.java
Created October 11, 2022 16:27
Configuration for using Twilio SDK for SMS Alert System
import com.twilio.Twilio;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
public class TwilioConfig {
@Value("${twilio.account.sid}")
@viveknaskar
viveknaskar / pom.xml
Last active October 11, 2022 13:27
Dependencies for SMS Alert System Using Twilio
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
@viveknaskar
viveknaskar / ExceptionallyExample.java
Created September 22, 2022 21:41
Illustration of exceptionally method of CompletableFuture
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class ExceptionallyExample {
public static void main(String[] args) throws ExecutionException, InterruptedException {
int val = 0;
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
if(val == 0) {
throw new IllegalArgumentException("Value is 0");
}
@viveknaskar
viveknaskar / ThenCombineExample.java
Last active September 22, 2022 21:37
Illustration of thenCombine method of CompletableFuture
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class ThenCombineExample {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
try { // delaying the thread by 2 seconds
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
@viveknaskar
viveknaskar / ThenComposeExample.java
Last active September 22, 2022 20:44
Illustration of thenCompose method of CompletableFuture
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class ThenComposeExample {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
try { // delaying the thread by 2 seconds
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
@viveknaskar
viveknaskar / ThenRunExample.java
Created September 20, 2022 22:28
Illustration of thenRun method of CompletableFuture
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class ThenRunExample {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Void> completableFuture = CompletableFuture.supplyAsync(() -> {
try {
// delaying the thread by 2 seconds
TimeUnit.SECONDS.sleep(2);
@viveknaskar
viveknaskar / ThenAcceptExample.java
Last active September 20, 2022 22:23
Illustration of thenAccept method chaining for CompletableFuture
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
public class ThenAcceptExample {
public static void main(String[] args) throws ExecutionException, InterruptedException {
CompletableFuture<Void> completableFuture = CompletableFuture.supplyAsync(() -> {
try {
// delaying the thread by 2 seconds
TimeUnit.SECONDS.sleep(2);