file upload

parent 32db9244
...@@ -9,29 +9,29 @@ ...@@ -9,29 +9,29 @@
<relativePath/> <!-- lookup parent from repository --> <relativePath/> <!-- lookup parent from repository -->
</parent> </parent>
<groupId>com.example</groupId> <groupId>com.example</groupId>
<artifactId>number-service</artifactId> <artifactId>simpleNumber-service</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<name>number-service</name> <name>simpleNumber-service</name>
<description>Demo project for Spring Boot</description> <description>Demo project for Spring Boot</description>
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<spring-cloud.version>2021.0.3</spring-cloud.version> <spring-cloud.version>2021.0.3</spring-cloud.version>
</properties> </properties>
<dependencies> <dependencies>
<!-- <dependency>--> <dependency>
<!-- <groupId>org.springframework.boot</groupId>--> <groupId>org.springframework.boot</groupId>
<!-- <artifactId>spring-boot-starter-data-jpa</artifactId>--> <artifactId>spring-boot-starter-data-jpa</artifactId>
<!-- </dependency>--> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<!-- <dependency>--> <dependency>
<!-- <groupId>mysql</groupId>--> <groupId>mysql</groupId>
<!-- <artifactId>mysql-connector-java</artifactId>--> <artifactId>mysql-connector-java</artifactId>
<!-- <scope>runtime</scope>--> <scope>runtime</scope>
<!-- </dependency>--> </dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
......
...@@ -2,7 +2,10 @@ package com.example.number; ...@@ -2,7 +2,10 @@ package com.example.number;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableAsync;
@EnableFeignClients
@SpringBootApplication @SpringBootApplication
public class NumberServiceApplication { public class NumberServiceApplication {
......
package com.example.number.controller; package com.example.number.controller;
import org.springframework.web.bind.annotation.GetMapping; import com.example.number.model.SimpleNumber;
import org.springframework.web.bind.annotation.RequestMapping; import com.example.number.repository.NumberRepository;
import org.springframework.web.bind.annotation.RestController; import com.example.number.service.NumberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@Slf4j
@RestController @RestController
@RequestMapping("/number") @RequestMapping("/number")
public class NumberController { public class NumberController {
@GetMapping("/hello") @Autowired
public String hello() { private NumberService service;
return "Hello";
@Autowired
private NumberRepository repository;
// working ...
@PostMapping(value = "/submit-file", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}, produces = "application/json")
public String save(@RequestParam(value = "file") MultipartFile file ) throws Exception {
service.saveFile(file);
return "File saved";
}
// working ...
@GetMapping(value = "/simple-numbers", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}, produces = "application/json")
public List<SimpleNumber> getSimpleNumbers(@RequestParam(value = "file") MultipartFile file ) throws Exception {
List<CompletableFuture<SimpleNumber>> futureNumberList = service.getSimpleNumbers(file);
List<SimpleNumber> result = new ArrayList<>();
for (CompletableFuture<SimpleNumber> x: futureNumberList) {
result.add(x.get());
}
return result;
} }
} }
package com.example.number.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SquaredDto {
private int number;
private int squaredNumber;
}
package com.example.number.feign;
import com.example.number.model.SimpleNumber;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name = "square-service", url = "http://localhost:8081/square/find-square")
public interface FeignService {
@GetMapping("/{number}")
SimpleNumber squareNumber(@PathVariable("number") int number);
}
package com.example.number.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.PrimaryKeyJoinColumn;
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SimpleNumber {
@Id
@PrimaryKeyJoinColumn
private int number;
private int squaredNumber;
}
package com.example.number.repository;
import com.example.number.dto.SquaredDto;
import com.example.number.model.SimpleNumber;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface NumberRepository extends JpaRepository<SimpleNumber, Integer> {
}
package com.example.number.service;
import com.example.number.dto.SquaredDto;
import com.example.number.feign.FeignService;
import com.example.number.model.SimpleNumber;
import com.example.number.repository.NumberRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.*;
@Slf4j
@Service
public class NumberService {
@Autowired
private NumberRepository repository;
@Autowired
private FeignService feignService;
@Autowired
private SquareService service;
ExecutorService executor = Executors.newFixedThreadPool(3);
// private ExecutorService executorService;
// // working ...
// public void saveFile(final MultipartFile file) throws Exception {
// InputStreamReader myStream = new InputStreamReader(file.getInputStream());
// BufferedReader br = new BufferedReader(myStream);
// try {
// String line = null;
// line = br.readLine();
// List<Future<SimpleNumber>> futureNumberList = new ArrayList<>();
// while(line != null) {
// line = br.readLine();
// if (line != null) {
// int value = Integer.parseInt(line);
// log.info("calling squareNumber() in saveFile() with Thread " + Thread.currentThread().getName());
// Future<SimpleNumber> mySimpleFuture = executor.submit(() -> feignService.squareNumber(value));
// futureNumberList.add(mySimpleFuture);
// }
//
// }
// List<SimpleNumber> result = new ArrayList<>();
// for (Future<SimpleNumber> x: futureNumberList
// ) {
// result.add(x.get());
// }
// result.get(700).getNumber();
// List<SimpleNumber> result1 = repository.saveAll(result);
// log.info("Size" + result1.size());
// } catch (IOException | NumberFormatException e) {
// throw new RuntimeException(e);
// } finally {
// br.close();
// }
// }
// working...
public List<CompletableFuture<SimpleNumber>> getSimpleNumbers(final MultipartFile file) throws Exception {
InputStreamReader myStream = new InputStreamReader(file.getInputStream());
BufferedReader br = new BufferedReader(myStream);
Scanner sc = new Scanner(file.getInputStream());
List<CompletableFuture<SimpleNumber>> futureNumberList = new ArrayList<>();
try {
String line = null;
line = sc.nextLine();
while(sc.hasNext()) {
line = sc.nextLine();
if (line != null) {
int value = Integer.parseInt(line);
log.info("calling squareNumber() with Thread" + Thread.currentThread().getName());
}
}
return futureNumberList;
} catch (NumberFormatException e) {
throw new RuntimeException(e);
} finally {
br.close();
}
}
public void saveMany() {
List<SimpleNumber> numberList = new ArrayList<>();
for (int i = 0 ; i < 1000; i++) {
SimpleNumber number = new SimpleNumber(i, i * i);
numberList.add(number);
}
repository.saveAll(numberList);
}
// testing ...
public void saveFile(final MultipartFile file) throws Exception {
InputStreamReader myStream = new InputStreamReader(file.getInputStream());
BufferedReader br = new BufferedReader(myStream);
String line = null;
line = br.readLine();
List<SimpleNumber> futureNumberList = new ArrayList<>();
while(line != null) {
line = br.readLine();
if (line != null) {
int value = Integer.parseInt(line);
// log.info("calling squareNumber() in saveFile() with Thread " + Thread.currentThread().getName());
CompletableFuture<SimpleNumber> result = CompletableFuture.supplyAsync(() -> {
return feignService.squareNumber(value);
}, executor);
}
}
}
}
package com.example.number.service;
import com.example.number.dto.SquaredDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class SquareService {
public SquaredDto squareNumber(int number) throws InterruptedException {
log.info("Calling squareNumber method by Thread " + Thread.currentThread().getName());
return new SquaredDto(number, number * number);
}
}
server.port = 8082 server.port = 8082
spring.jpa.hibernate.ddl-auto=update
spring.jpa.defer-datasource-initialization=true
spring.sql.init.mode=always
spring.datasource.url=jdbc:mysql://localhost:3306/future
spring.datasource.username=root
spring.datasource.password=root1234
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql: true
feign.circuitbreaker.enabled=true
...@@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test; ...@@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest @SpringBootTest
class NumberServiceApplicationTests { class SimpleNumberServiceApplicationTests {
@Test @Test
void contextLoads() { void contextLoads() {
......
...@@ -27,6 +27,11 @@ ...@@ -27,6 +27,11 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies> </dependencies>
<build> <build>
...@@ -34,6 +39,14 @@ ...@@ -34,6 +39,14 @@
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
......
...@@ -2,6 +2,7 @@ package com.example.square; ...@@ -2,6 +2,7 @@ package com.example.square;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication @SpringBootApplication
public class SquareServiceApplication { public class SquareServiceApplication {
......
package com.example.square.controller; package com.example.square.controller;
import com.example.square.dto.SquaredDto;
import com.example.square.service.SquaredService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@RequestMapping("/square") @RequestMapping("/square")
public class SquareController { public class SquareController {
@Autowired
private SquaredService service;
@GetMapping("/hello") @GetMapping("/hello")
public String hello(){ public String hello(){
return "Hello"; return "Hello";
} }
@GetMapping("/find-square/{number}")
public SquaredDto findSquare(@PathVariable("number") int number) throws InterruptedException {
return service.squareNumber(number);
}
} }
package com.example.square.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SquaredDto {
private int number;
private int squaredNumber;
}
package com.example.square.service;
import com.example.square.dto.SquaredDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class SquaredService {
// ExecutorService executor = Executors.newFixedThreadPool(10);
// public Future<SquaredDto> squareNumber(int number) {
// log.info("Calling squareNumber method by Thread " + Thread.currentThread().getName());
// return executor.submit(() -> {
// int squared = number * number;
// return new SquaredDto(number, squared);
// });
// }
public SquaredDto squareNumber(int number) throws InterruptedException {
log.info("Calling squareNumber method by Thread " + Thread.currentThread().getName());
return new SquaredDto(number, number * number);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment