gpt4 book ai didi

java - Spring Scheduler 示例(Spring boot 和 Vaadin 14 项目)

转载 作者:行者123 更新时间:2023-12-02 09:05:51 25 4
gpt4 key购买 nike

我创建了一个用于通信的聊天。对于我使用vaadin的界面,我想测试聊天,需要当聊天打开时,它需要每1秒启动一次(hello 1, hello 2, hello 3 .. .....)我写了一个调度程序,但是它怎么可能是正确的呢?我好像写错了。

主视图

    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();
}




@Scheduled (fixedDelay = 1000)
public void test() {
System.out.println("Hello");
}
}

Mainview 中的调度程序

    @Scheduled (fixedDelay = 1000)
public void test() {
System.out.println("Hello");
}

我的例子

@Scheduled (fixedDelay = 1000)
public void test() {
count++;

System.out.println("Hello"+count);


}

最佳答案

启用调度

您只需将 @EnableScheduling 注解添加到主应用程序类或任何配置类即可启用调度。

安排任务

计划任务就像使用 @Scheduled 注释来注释方法一样简单。

在下面的示例中,execute() 方法计划每秒运行一次。在此示例中,execute() 方法应调用所需的服务方法,例如 getAllMessages()。

@EnableScheduling
public class MainView extends ... {

// Existing Code

@Autowired
private MessageServiceImpl messageService;

@Scheduled(fixedRate = 1000)
public void execute() {
messageService.getAllMessages();
}

}

调度类型

  1. 以固定费率安排

    execute() method can be scheduled to run with a fixed interval using fixedRate parameter.

    @Scheduled(fixedRate = 2000)
  2. 固定延迟调度

    execute() method can be scheduled to run with a fixed delay between the completion of the last invocation and the start of the next, using fixedDelay parameter.

    @Scheduled(fixedDelay = 2000)
  3. 具有初始延迟和固定速率/固定延迟的调度

    initialDelay parameter with fixedRate and fixedDelay to delay the first execution.

    @Scheduled(fixedRate = 2000, initialDelay = 5000)
    @Scheduled(fixedDelay= 2000, initialDelay = 5000)
  4. 使用 cron 进行调度

    execute() method can be scheduled to run based on cron expression using cron parameter.

    @Scheduled(cron = "0 * * * * *")

关于java - Spring Scheduler 示例(Spring boot 和 Vaadin 14 项目),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59816888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com