mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
examples updated
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.3.3.RELEASE</version>
|
||||
<version>2.4.1</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
|
||||
+6
-3
@@ -1,9 +1,11 @@
|
||||
package ru.otus.ioservice.example.console;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.otus.ioservice.example.api.IOService;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.util.Scanner;
|
||||
|
||||
@@ -14,9 +16,10 @@ public class ConsoleIOService implements IOService {
|
||||
private final Scanner sc;
|
||||
|
||||
|
||||
public ConsoleIOService() {
|
||||
this.out = System.out;
|
||||
this.sc = new Scanner(System.in);
|
||||
public ConsoleIOService(@Value("#{T(java.lang.System).out}") PrintStream out,
|
||||
@Value("#{T(java.lang.System).in}")InputStream in) {
|
||||
this.out = out;
|
||||
this.sc = new Scanner(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ public class PollService {
|
||||
private final IOService ioService;
|
||||
|
||||
public void poll() {
|
||||
System.out.println("Началось!");
|
||||
ioService.out("Началось!");
|
||||
|
||||
ioService.out("Как вас зовут?");
|
||||
String name = ioService.readString();
|
||||
|
||||
+12
-12
@@ -9,31 +9,31 @@ import java.util.concurrent.LinkedBlockingQueue;
|
||||
@ConditionalOnBean(SwingIOService.class)
|
||||
@Service
|
||||
public class MessageSystem {
|
||||
private final LinkedBlockingQueue<String> pollQueue;
|
||||
private final LinkedBlockingQueue<String> uiQueue;
|
||||
private final LinkedBlockingQueue<String> inputQueue;
|
||||
private final LinkedBlockingQueue<String> outputQueue;
|
||||
|
||||
public MessageSystem() {
|
||||
pollQueue = new LinkedBlockingQueue<>();
|
||||
uiQueue = new LinkedBlockingQueue<>();
|
||||
inputQueue = new LinkedBlockingQueue<>();
|
||||
outputQueue = new LinkedBlockingQueue<>();
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public void putToPollQueue(String message) {
|
||||
pollQueue.put(message);
|
||||
public void putToInputQueue(String message) {
|
||||
inputQueue.put(message);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public void putToUiQueue(String message) {
|
||||
uiQueue.put(message);
|
||||
public void putToOutputQueue(String message) {
|
||||
outputQueue.put(message);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public String takeFromPollQueue() {
|
||||
return pollQueue.take();
|
||||
public String takeFromInputQueue() {
|
||||
return inputQueue.take();
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public String takeFromUiQueue() {
|
||||
return uiQueue.take();
|
||||
public String takeFromOutputQueue() {
|
||||
return outputQueue.take();
|
||||
}
|
||||
}
|
||||
|
||||
+6
-4
@@ -14,6 +14,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
@RequiredArgsConstructor
|
||||
public class PollMainForm extends JFrame {
|
||||
|
||||
private static final int MESSAGE_DISPLAY_DURATION = 1500;
|
||||
|
||||
private final JPanel contentPane;
|
||||
private final JLabel inLabel;
|
||||
private final JTextField outTextField;
|
||||
@@ -51,7 +53,7 @@ public class PollMainForm extends JFrame {
|
||||
contentPane.add(outTextField);
|
||||
|
||||
outBtn = new JButton("Ответить");
|
||||
outBtn.addActionListener(e -> ms.putToPollQueue(outTextField.getText()));
|
||||
outBtn.addActionListener(e -> ms.putToInputQueue(outTextField.getText()));
|
||||
contentPane.add(outBtn);
|
||||
setOnCloseHandler();
|
||||
setSize(450, 180);
|
||||
@@ -67,11 +69,11 @@ public class PollMainForm extends JFrame {
|
||||
uiFlow.execute(() -> {
|
||||
while (runFlag.get()) {
|
||||
try {
|
||||
String msg = ms.takeFromUiQueue();
|
||||
String msg = ms.takeFromOutputQueue();
|
||||
synchronized (inLabel) {
|
||||
inLabel.setText(msg);
|
||||
}
|
||||
Thread.sleep(1000);
|
||||
Thread.sleep(MESSAGE_DISPLAY_DURATION);
|
||||
} catch (InterruptedException e) {
|
||||
return;
|
||||
}
|
||||
@@ -85,7 +87,7 @@ public class PollMainForm extends JFrame {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
runFlag.set(false);
|
||||
ms.putToUiQueue("exit");
|
||||
ms.putToOutputQueue("exit");
|
||||
uiFlow.shutdown();
|
||||
e.getWindow().dispose();
|
||||
}
|
||||
|
||||
+2
-2
@@ -15,12 +15,12 @@ public class SwingIOService implements IOService {
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public void out(String message) {
|
||||
ms.putToUiQueue(message);
|
||||
ms.putToOutputQueue(message);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public String readString() {
|
||||
return ms.takeFromPollQueue();
|
||||
return ms.takeFromInputQueue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<lombok.version>1.18.16</lombok.version>
|
||||
<spring.version>5.3.1</spring.version>
|
||||
<spring.version>5.3.2</spring.version>
|
||||
<junit.jupiter.version>5.7.0</junit.jupiter.version>
|
||||
<mockito.version>3.6.28</mockito.version>
|
||||
<assertj.version>3.18.1</assertj.version>
|
||||
|
||||
Reference in New Issue
Block a user