title: Spring Scheduler example (Spring boot and Vaadin 14 project)
body: <p>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? I seem to have written it wrong. </p>
  
  <p><code>MainView</code></p>
  
  <pre class="lang-java prettyprint-override"><code>    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 -&gt; {
              username = usernameField.getValue();
              remove(layout);
              showChat(username);
          });
  
          add(layout);
      }
  
      private void showChat(String username) {
          MessageList messageList = new MessageList();
  
          List&lt;Message&gt; 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 -&gt; sender(messageField, messageList));
          Button sendButton = new Button("Send");
          sendButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
  
          layout.add(messageField, sendButton);
          layout.expand(messageField);
  
          messageField.addFocusListener(event -&gt; {
              for (Message message : messagesInfoManager.getMessagesByUI(getUI())) {
                  if (!message.getFrom().equals(username)) {
                      message.setUnread(false);
                      this.restService.updateMessage(message.getId(), message);
                  }
              }
          });
  
          sendButton.addClickListener(click -&gt; 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");
      }
      }
  </code></pre>
  
  <p>Scheduler in Mainview</p>
  
  <pre><code>    @Scheduled (fixedDelay = 1000)
      public void test() {
      System.out.println("Hello");
      }
  
  </code></pre>
  
  <p>My example</p>
  
  <pre><code>@Scheduled (fixedDelay = 1000)
  public void test() {
      count++; 
  
      System.out.println("Hello"+count);
  
  
  }
  </code></pre>