Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
java-training-project-3
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Muhammad Abdul Qadeer Farooqui
java-training-project-3
Commits
4062515f
Commit
4062515f
authored
2 years ago
by
Muhammad Abdul Qadeer Farooqui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
file upload
parent
32db9244
Changes
16
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
319 additions
and
19 deletions
+319
-19
pom.xml
number-service/pom.xml
+11
-11
NumberServiceApplication.java
...ain/java/com/example/number/NumberServiceApplication.java
+3
-0
NumberController.java
.../java/com/example/number/controller/NumberController.java
+37
-7
SquaredDto.java
...vice/src/main/java/com/example/number/dto/SquaredDto.java
+13
-0
FeignService.java
.../src/main/java/com/example/number/feign/FeignService.java
+13
-0
SimpleNumber.java
.../src/main/java/com/example/number/model/SimpleNumber.java
+20
-0
NumberRepository.java
.../java/com/example/number/repository/NumberRepository.java
+10
-0
NumberService.java
...c/main/java/com/example/number/service/NumberService.java
+122
-0
SquareService.java
...c/main/java/com/example/number/service/SquareService.java
+14
-0
application.properties
number-service/src/main/resources/application.properties
+10
-0
SimpleNumberServiceApplicationTests.java
...m/example/number/SimpleNumberServiceApplicationTests.java
+1
-1
pom.xml
square-service/pom.xml
+13
-0
SquareServiceApplication.java
...ain/java/com/example/square/SquareServiceApplication.java
+1
-0
SquareController.java
.../java/com/example/square/controller/SquareController.java
+14
-0
SquaredDto.java
...vice/src/main/java/com/example/square/dto/SquaredDto.java
+13
-0
SquaredService.java
.../main/java/com/example/square/service/SquaredService.java
+24
-0
No files found.
number-service/pom.xml
View file @
4062515f
...
@@ -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>
n
umber-service
</artifactId>
<artifactId>
simpleN
umber-service
</artifactId>
<version>
0.0.1-SNAPSHOT
</version>
<version>
0.0.1-SNAPSHOT
</version>
<name>
n
umber-service
</name>
<name>
simpleN
umber-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>
...
...
This diff is collapsed.
Click to expand it.
number-service/src/main/java/com/example/number/NumberServiceApplication.java
View file @
4062515f
...
@@ -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
{
...
...
This diff is collapsed.
Click to expand it.
number-service/src/main/java/com/example/number/controller/NumberController.java
View file @
4062515f
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
;
}
}
}
}
This diff is collapsed.
Click to expand it.
number-service/src/main/java/com/example/number/dto/SquaredDto.java
0 → 100644
View file @
4062515f
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
;
}
This diff is collapsed.
Click to expand it.
number-service/src/main/java/com/example/number/feign/FeignService.java
0 → 100644
View file @
4062515f
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
);
}
This diff is collapsed.
Click to expand it.
number-service/src/main/java/com/example/number/model/SimpleNumber.java
0 → 100644
View file @
4062515f
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
;
}
This diff is collapsed.
Click to expand it.
number-service/src/main/java/com/example/number/repository/NumberRepository.java
0 → 100644
View file @
4062515f
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
>
{
}
This diff is collapsed.
Click to expand it.
number-service/src/main/java/com/example/number/service/NumberService.java
0 → 100644
View file @
4062515f
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
);
}
}
}
}
This diff is collapsed.
Click to expand it.
number-service/src/main/java/com/example/number/service/SquareService.java
0 → 100644
View file @
4062515f
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
);
}
}
This diff is collapsed.
Click to expand it.
number-service/src/main/resources/application.properties
View file @
4062515f
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
This diff is collapsed.
Click to expand it.
number-service/src/test/java/com/example/number/NumberServiceApplicationTests.java
→
number-service/src/test/java/com/example/number/
Simple
NumberServiceApplicationTests.java
View file @
4062515f
...
@@ -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
Simple
NumberServiceApplicationTests
{
@Test
@Test
void
contextLoads
()
{
void
contextLoads
()
{
...
...
This diff is collapsed.
Click to expand it.
square-service/pom.xml
View file @
4062515f
...
@@ -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>
...
...
This diff is collapsed.
Click to expand it.
square-service/src/main/java/com/example/square/SquareServiceApplication.java
View file @
4062515f
...
@@ -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
{
...
...
This diff is collapsed.
Click to expand it.
square-service/src/main/java/com/example/square/controller/SquareController.java
View file @
4062515f
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
);
}
}
}
This diff is collapsed.
Click to expand it.
square-service/src/main/java/com/example/square/dto/SquaredDto.java
0 → 100644
View file @
4062515f
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
;
}
This diff is collapsed.
Click to expand it.
square-service/src/main/java/com/example/square/service/SquaredService.java
0 → 100644
View file @
4062515f
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
);
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment