简体   繁体   English

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

[英]Spring Scheduler example (Spring boot and Vaadin 14 project)

I created a chat for communication.For the interface I use vaadin, I want to test the chat, it's necessary that when the chat opens, it needs to start every 1 second (hello 1, hello 2, hello 3 .......) I wrote a sheduler, but how can it be correct?我创建了一个聊天进行交流。对于我使用vaadin的界面,我想测试一下聊天,有必要在聊天打开的时候,每1秒开始一次(你好1,你好2,你好3 ..... ..) 我写了一个 sheduler,但它怎么可能是正确的? I seem to have written it wrong.我好像写错了。

MainView

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

Scheduler in Mainview Mainview 中的调度程序

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

My example我的例子

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

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


}

Enable Scheduling启用调度

You can enable scheduling simply by adding the @EnableScheduling annotation to the main application class or any configuration class.您只需将@EnableScheduling注释添加到主应用程序类或任何配置类即可启用调度。

Scheduling Tasks调度任务

Scheduling a task is as simple as annotating a method with @Scheduled annotation.调度任务就像使用@Scheduled 注释来注释方法一样简单。

In the below example, execute() method is scheduled to run every second.在下面的示例中,execute() 方法被安排为每秒运行一次。 execute() method should invoke desired service method lke getAllMessages() in this example.在此示例中,execute() 方法应调用所需的服务方法,例如 getAllMessages()。

@EnableScheduling
public class MainView extends ... {

    // Existing Code

    @Autowired
    private MessageServiceImpl messageService;

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

}

Types of Scheduling调度类型

  1. Scheduling with fixed rate固定费率调度

    execute() method can be scheduled to run with a fixed interval using fixedRate parameter.可以使用fixedRate参数安排execute()方法以固定间隔运行。

     @Scheduled(fixedRate = 2000)
  2. Scheduling with fixed delay固定延迟调度

    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.可以使用fixedDelay参数将execute()方法安排为在上次调用完成和下一次调用开始之间以固定延迟运行。

     @Scheduled(fixedDelay = 2000)
  3. Scheduling with initial delay and fixed rate / fixed delay具有初始延迟和固定速率/固定延迟的调度

    initialDelay parameter with fixedRate and fixedDelay to delay the first execution.带有fixedRatefixedDelay initialDelay参数来延迟第一次执行。

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

    execute() method can be scheduled to run based on cron expression using cron parameter.可以使用cron参数根据 cron 表达式安排execute()方法运行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM