public void start() {
stopwatch.start();
}
@Component или @Service
@Component
public class Qtask extends TimerTask {
private String xtime;
public int hours = 0;
public int minutes = 0;
public int seconds = 0;
public int elapsedTime = 0;
boolean started = false;
@Override
public void run() {
elapsedTime=elapsedTime+1000;
hours = (elapsedTime/3600000);
minutes = (elapsedTime/60000)%60;
seconds = (elapsedTime/1000)%60
// TODO :: set xtime
xtime = String.valueOf(elapsedTime);
}
public String getXtime() {
return this.xtime;
}
}
@Component
public class SocketTextHandler extends TextWebSocketHandler {
@Autowired
private Qtask qtask;
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message)
throws InterruptedException, IOException {
String clientMessage = message.getPayload();
if (clientMessage.startsWith("start")) {
new Timer().scheduleAtFixedRate(qtask, 0, 1000);
try {
Thread.sleep(1000);
session.sendMessage(new TextMessage(qtask.getXtime()));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Main {
public static void main(String[] args) {
A b = Fabric.getInstance("B");
A c = Fabric.getInstance("C");
System.out.println(b.getString());
System.out.println(c.getString());
}
}
class Fabric {
public static A getInstance(String className) {
return switch (className) {
case "B" -> new B();
case "C" -> new C();
default -> throw new IllegalArgumentException("Unknown class name " + className);
};
}
}
interface A {
String getString();
}
class B implements A {
@Override
public String getString() {
return "bbb";
}
}
class C implements A {
@Override
public String getString() {
return "ccc";
}
}
В школе окружение хреновое