简体   繁体   English

在 vaadin 聊天对话框中输出 hello(Spring boot 和 spring scheduler + vaadin 14)

[英]Output hello in vaadin chat dialog (Spring boot and spring scheduler + vaadin 14)

I created a chat on the vaadine and spring boot.我创建了一个关于 vaadine 和弹簧靴的聊天。 So I have a problem with the scheduler, I wanted to write a scheduler that displays "hello" every second, but how can I write so that it calls hello every second, only it was like this Example output -所以我的调度程序有问题,我想编写一个每秒显示“hello”的调度程序,但是我怎么写才能让它每秒调用 hello ,就像这个示例输出 -

hello 1 - (when 1 second has passed)
hello 2 - (after two seconds)
hello 3 - (after three seconds)
.
.
.
.
hello 45 - (after 45 seconds)
.
.
.
.
hello 100 - (after 100 seconds)

Image example图片示例在此处输入图片说明

My code, How can I launch a sсheduler in a chat when I log in我的代码,登录时如何在聊天中启动调度程序

public class MainView extends VerticalLayout {

    private final MessagesInfoManager messagesInfoManager;
    private final RestService restService;
    private String username;



     private int count=0;

    @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() {
    count++; 
    System.out.println("Hello"+count);

}
}

In the compiler displays, now i need to chat在编译器显示中,现在我需要聊天在此处输入图片说明

How you put up a scheduler in Java is not much related to Vaadin in itself.您如何在 Java 中设置调度程序与 Vaadin 本身没有太大关系。 You can use a second thread that you run, or as you seem to have, a readymade tool for it.您可以使用您运行的第二个线程,或者您似乎拥有的现成工具。 Looks like a Spring feature if I'm not mistaken.如果我没记错的话,它看起来像一个 Spring 功能。

After that, you update the Vaadin components to contain the new messages.之后,您更新 Vaadin 组件以包含新消息。 You can for example make your messageList into a field and within test() add a new paragraph to it.例如,您可以将 messageList 放入一个字段并在test()添加一个新段落。 Or use the backend service that you have to push messages in from there.或者使用您必须从那里推送消息的后端服务。

When you do update the UI from a secondary thread, remember that you might run into race conditions.当您从辅助线程更新 UI 时,请记住您可能会遇到竞争条件。 For that reason, you should lock the user session for the time that you make UI changes .因此,您应该在进行 UI 更改时锁定用户会话

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

        this.getUI().get().access(() -> {

            if (messageList != null) {
                messageList.add(new Paragraph("From me" + ": " + "Hello" + count));
            }

        });

    }
}

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

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