我使用vaadin框架14和spring boot创建了一个聊天室。它运作良好,只需要为它创建一个计时器。我创建了一个计时器,但被告知这是错误的。我使用Java创建了一个计时器,这是错误的。我需要在“ MainView”类中使用(Spring中的@Scheduled注解)。这样他每秒钟调用一次(api /未读)。
我以前创建了一个类“ TimerConfig”。但是他们说错了
public class TimerConfig {
@Autowired
MessageServiceImpl messageService;
@Bean
public TimerTask timer () {
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
messageService.getAllMessages();
}
};
Timer timer = new Timer();
timer.schedule(timerTask, 1000, 1000);
return timerTask;
}
}
需要在此类“ MainView”中创建(Spring中@Scheduled Annotation),以便计时器每秒调用(api /未读)
MainView类
@StyleSheet("frontend://styles/styles.css")
@Route
@PWA(name = "Vaadin MessagesInfoManager", shortName = "Vaadin MessagesInfoManager")
@Push
public class MainView extends VerticalLayout {
private final MessagesInfoManager messagesInfoManager;
private final RestService restService;
private String username;
@Autowired
public MainView(RestService restService) {
this.messagesInfoManager = MessageConfigurator.getInstance().getChatMessagesInfoManager();
addClassName("main-view");
setSizeFull();
setDefaultHorizontalComponentAlignment(Alignment.CENTER);
H1 header = new H1("Vaadin Chat");
header.getElement().getThemeList().add("dark");
add(header);
askUsername();
this.restService = restService;
}
private void askUsername() {
HorizontalLayout layout = new HorizontalLayout();
TextField usernameField = new TextField();
Button startButton = new Button("Start chat");
layout.add(usernameField, startButton);
startButton.addClickListener(click -> {
username = usernameField.getValue();
remove(layout);
showChat(username);
});
add(layout);
}
private void showChat(String username) {
MessageList messageList = new MessageList();
List<Message> lasts = restService.getLast();
for (Message message : lasts) {
messageList.add(new Paragraph(message.getFrom() + ": " + message.getMessage()));
}
add(messageList, createInputLayout(username, messageList));
expand(messageList);
}
private Component createInputLayout(String username, MessageList messageList) {
HorizontalLayout layout = new HorizontalLayout();
layout.setWidth("100%");
TextField messageField = new TextField();
messageField.addKeyDownListener(Key.ENTER, keyDownEvent -> sender(messageField, messageList));
Button sendButton = new Button("Send");
sendButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
layout.add(messageField, sendButton);
layout.expand(messageField);
messageField.addFocusListener(event -> {
for (Message message : messagesInfoManager.getMessagesByUI(getUI())) {
if (!message.getFrom().equals(username)) {
message.setUnread(false);
this.restService.updateMessage(message.getId(), message);
}
}
});
sendButton.addClickListener(click -> sender(messageField, messageList));
messageField.focus();
return layout;
}
private void sender(TextField textField, MessageList messageList) {
Message message = new Message(username, textField.getValue());
message = restService.saveMessage(message);
messagesInfoManager.updateMessageUIInfo(new MessageInfo(messageList, message, this));
textField.clear();
textField.focus();
}
}
我的休息控制器
public class RestController {
@Autowired
TimerTask timerTask;
@Resource
private final MessageService messageService;
public RestController(MessageService messageService) {
this.messageService = messageService;
}
@GetMapping("/api/unread")
public void getUnreadMessages() {
timerTask.run(); // it's wrong
}
启用调度
您可以简单地通过将@EnableScheduling
批注添加到主应用程序类或任何配置类来启用调度。
计划任务
安排任务就像用@Scheduled
注释对方法进行注释一样简单。
在下面的示例中,execute()
方法被安排为每秒运行一次。 execute()
方法将调用所需的服务方法。
public class MainView extends ... {
// Existing Code
@Autowired
private MessageServiceImpl messageService;
@Scheduled(fixedRate = 1000)
public void execute() {
messageService.getAllMessages();
}
}
计划类型
固定费率计划
[
execute()
方法可以安排为使用fixedRate
参数以固定间隔运行。@Scheduled(fixedRate = 2000)
具有固定延迟的计划
[
execute()
方法可以安排为使用fixedDelay
参数在上一次调用完成与下一次调用开始之间以固定的延迟运行。@Scheduled(fixedDelay = 2000)
具有初始延迟和固定速率/固定延迟的计划
[
initialDelay
参数与fixedRate
和fixedDelay
一起延迟第一次执行。@Scheduled(fixedRate = 2000, initialDelay = 5000)
@Scheduled(fixedDelay= 2000, initialDelay = 5000)
使用cron进行计划
[
execute()
方法可以安排为使用cron
参数基于cron表达式运行。@Scheduled(cron = "0 * * * * *")
将@Scheduled Anno保留在getAllMessages()方法serviceImpl的顶部]>
public class MessageServiceimpl {
@Scheduled(fixedDelay = 1500000)// 25 min
public void getAllMessages(){
---------------
--- your implementations
---------------
}
}
public class MainView extends VerticalLayout {
@Autowired
MessageServiceImpl messageService;
----
---- your code here----
----
@Scheduled(fixedRate = 1000) (OR @Scheduled(cron = "0 * * * * *"))
void getMessagesBySchedule(){
messageService.getAllMessages();
{