DBILITY

vaadin Polling 실습 본문

front-end & ui/vaadin legacy

vaadin Polling 실습

DBILITY 2018. 6. 17. 19:32
반응형

로그인 실습 UI에 PollListener를 등록하고 해 보았다.

UI Polling disable처리는 -1을 argument로 넘기면 된다.

 

 

package com.dbility.vseminar.vaadin_seminar;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Set;

import javax.servlet.annotation.WebServlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dbility.vseminar.vaadin_seminar.util.RandomUtil;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.Title;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.event.UIEvents.PollEvent;
import com.vaadin.event.UIEvents.PollListener;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinService;
import com.vaadin.server.VaadinServlet;
import com.vaadin.server.WrappedSession;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.ValoTheme;

/**
 * This UI is the application entry point. A UI may either represent a browser window
 * (or tab) or some part of an HTML page where a Vaadin application is embedded.
 * <p>
 * The UI is initialized using {@link #init(VaadinRequest)}. This method is intended to be
 * overridden to add component to the user interface and initialize non-component functionality.
 */
@SuppressWarnings("serial")
@Title("vseminar")
@Theme("vseminar")
public class VSeminarUI extends UI {

	private static final Logger logger = LoggerFactory.getLogger(VSeminarUI.class);
	private Label message = new Label();

    @Override
    protected void init(VaadinRequest vaadinRequest) {

    	VaadinRequest request = VaadinService.getCurrentRequest();

    	final WrappedSession session = request.getWrappedSession();

    	//WrappedSession session = vaadinRequest.getWrappedSession();
    	final VerticalLayout box = new VerticalLayout();
    	box.setSpacing(true);
    	box.setMargin(true);

    	final HorizontalLayout layout = new HorizontalLayout();
		layout.setSizeUndefined();

    	if(session == null) {
    		logger.debug("{}", session);
    	} else {
    		Set<String> set = session.getAttributeNames();
    		for (String str : set) {
    			logger.debug("{}", str);
			}
    		logger.debug("{}", session.getId());
    		logger.debug("{}", session.getLastAccessedTime());
    		logger.debug("{}", session.getMaxInactiveInterval());
    		logger.debug("{}", session.getAttribute("VSeminarUIServlet.lock"));
    		logger.debug("{}", session.getAttribute("com.vaadin.server.VaadinSession.VSeminarUIServlet"));

    		box.addComponent(message);

    		if(session.getAttribute("SESSID")==null) {
    			logger.debug("{}", "로그인해야지");
    			Label userMessage = new Label("로그인해야지");
    			userMessage.setSizeUndefined();
    			userMessage.setStyleName(ValoTheme.LABEL_H1);
    			userMessage.setStyleName(ValoTheme.LABEL_COLORED);
    			layout.addComponent(userMessage);

    			Button signon = new Button("SignOn");
    			signon.setSizeUndefined();
    			signon.setStyleName(ValoTheme.BUTTON_PRIMARY);
    			signon.addClickListener(e->{
    				session.setAttribute("SESSID", RandomUtil.randomKey());
    				getPage().reload();
    			});
    			layout.addComponent(signon);
    			layout.setComponentAlignment(signon, Alignment.BOTTOM_LEFT);
    		} else {

    			logger.debug("{}", session.getAttribute("SESSID"));

    			Label userName = new Label();
    			userName.setSizeUndefined();
    			userName.setStyleName(ValoTheme.LABEL_H1);
    			userName.setStyleName(ValoTheme.LABEL_COLORED);
    			userName.setValue("UserName : " + session.getAttribute("SESSID").toString());
    			layout.addComponent(userName);

    			Button signout = new Button("SignOut");
    			signout.setSizeUndefined();
    			signout.setStyleName(ValoTheme.BUTTON_PRIMARY);
    			signout.addClickListener(e->{
    				session.invalidate();
    				getPage().reload();
    			});

    			layout.addComponent(signout);
    			layout.setComponentAlignment(signout, Alignment.BOTTOM_LEFT);

   				getCurrent().setPollInterval(1000);

    			addPollListener(new PollListener() {

					@Override
					public void poll(PollEvent event) {
						new Poller().start();
					}
				});

    		}
    	}
    	box.addComponent(layout);
		setContent(box);

		logger.debug("{}", getPollInterval());
    }

    @WebServlet(urlPatterns = "/*", name = "VSeminarUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = VSeminarUI.class, productionMode = false)
    public static class VSeminarUIServlet extends VaadinServlet {
    }

    class Poller extends Thread {

		@Override
		public void run() {
			access(new Runnable() {

				@Override
				public void run() {
					String key = RandomUtil.randomKey();
					message.setValue(String.format("%s , %s", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")), key));
					logger.debug("time {}, key {}", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")), key);
				}
			});
		}

    }
}
DEBUG          c.d.v.vaadin_seminar.VSeminarUI  148 - time 2018-06-17 19:31:34.904, key pq3a-2y8y4mpg-pesl-519t
DEBUG          c.d.v.vaadin_seminar.VSeminarUI  148 - time 2018-06-17 19:31:35.901, key 73dm-rdg28ny2-3cg9-9gr9
DEBUG          c.d.v.vaadin_seminar.VSeminarUI  148 - time 2018-06-17 19:31:36.902, key ghzp-4q5id80b-gsju-u68p
DEBUG          c.d.v.vaadin_seminar.VSeminarUI  148 - time 2018-06-17 19:31:37.900, key 5m4v-ux6wpm9a-9yy7-cthc
DEBUG          c.d.v.vaadin_seminar.VSeminarUI  148 - time 2018-06-17 19:31:38.902, key c75w-fkn68wvf-m5xm-ernr
DEBUG          c.d.v.vaadin_seminar.VSeminarUI  148 - time 2018-06-17 19:31:39.901, key qtw2-9u2mr8ld-1afq-u6dj
DEBUG          c.d.v.vaadin_seminar.VSeminarUI  148 - time 2018-06-17 19:31:40.901, key tvkw-07ymmvg2-wkj1-lkme
DEBUG          c.d.v.vaadin_seminar.VSeminarUI  148 - time 2018-06-17 19:31:41.900, key cez5-dlcztrys-6fxd-90is
DEBUG          c.d.v.vaadin_seminar.VSeminarUI  148 - time 2018-06-17 19:31:42.900, key qd4x-7vejk6vv-ns2x-vbai
DEBUG          c.d.v.vaadin_seminar.VSeminarUI  148 - time 2018-06-17 19:31:43.901, key eidn-isskjhds-f5tk-z7qj
DEBUG          c.d.v.vaadin_seminar.VSeminarUI  148 - time 2018-06-17 19:31:44.901, key 7zde-wsq10m57-yb6y-mud7
DEBUG          c.d.v.vaadin_seminar.VSeminarUI  148 - time 2018-06-17 19:31:45.900, key w9zx-h71cpv25-pe4e-fxg1
DEBUG          c.d.v.vaadin_seminar.VSeminarUI  148 - time 2018-06-17 19:31:46.903, key v4fy-tz6hi4yt-64dk-rjp8
DEBUG          c.d.v.vaadin_seminar.VSeminarUI  148 - time 2018-06-17 19:31:47.901, key 7rqr-bpom84n0-xz6r-jhkl

 

반응형

'front-end & ui > vaadin legacy' 카테고리의 다른 글

vaadin Layout 수동 교체?  (0) 2018.06.18
vaadin Navigator 실습  (0) 2018.06.18
vaadin session 실습  (0) 2018.06.17
vaadin layout 실습  (0) 2018.06.16
vaadin field validation 실습2  (0) 2018.06.16
Comments