Skip to content

Instantly share code, notes, and snippets.

@oxlade39
Last active December 16, 2015 21:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oxlade39/5498131 to your computer and use it in GitHub Desktop.
Save oxlade39/5498131 to your computer and use it in GitHub Desktop.
Example of Vaadin 7.1 server push with rxjava
package com.github.oxlade39.vaadin;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;
import rx.subjects.Subject;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* @author dan
*/
public class Service {
private static final Logger LOGGER = LoggerFactory.getLogger(Service.class);
private static final int DELAY = 100;
private final ScheduledExecutorService executorService;
private final Subject<DateTime> dateTimeSubject = Subject.create();
public Service(ScheduledExecutorService executorService) {
this.executorService = executorService;
executorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
DateTime dateTime = new DateTime();
LOGGER.debug("publishing {}", dateTime);
dateTimeSubject.onNext(dateTime);
}
}, DELAY, DELAY, TimeUnit.MILLISECONDS);
}
public Observable<DateTime> time() {
return dateTimeSubject;
}
}
package com.github.oxlade39.vaadin;
import com.vaadin.annotations.Title;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Button;
import com.vaadin.ui.Table;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;
import rx.Subscription;
import rx.util.functions.Action1;
import static com.github.oxlade39.rx.Actions.runActionSafely;
import static com.github.oxlade39.rx.ToStringFunction.toStringF;
/**
* @author dan
*/
@Title("Welcome")
public class View extends UI {
private static Logger LOGGER = LoggerFactory.getLogger(View.class);
private final VerticalLayout verticalLayout = new VerticalLayout();
private Table table = new Table("My Table");
private Button toggle = new Button("Start");
private IndexedContainer dataSource = new IndexedContainer();
@Override
protected void init(VaadinRequest request) {
setContent(verticalLayout);
verticalLayout.addComponent(table);
verticalLayout.addComponent(toggle);
dataSource.addContainerProperty("name", String.class, "");
table.setContainerDataSource(dataSource);
final Service service = ((CustomVaadinServletService) request.getService()).getService();
toggle.addClickListener(new SubscriptionClickListener(service, dataSource));
}
private class SubscriptionClickListener implements Button.ClickListener {
private final Service service;
private final IndexedContainer dataSource;
private Subscription subscription;
public SubscriptionClickListener(Service service, IndexedContainer dataSource) {
this.service = service;
this.dataSource = dataSource;
}
@Override
public void buttonClick(Button.ClickEvent clickEvent) {
LOGGER.debug("buttonClick {}", clickEvent);
String caption = clickEvent.getButton().getCaption();
if ("Start".equals(caption)) {
subscribe(clickEvent);
} else {
cancelSubscription(clickEvent);
}
}
private void subscribe(Button.ClickEvent clickEvent) {
LOGGER.debug("Starting subscription");
Observable<DateTime> time = service.time();
subscription = time.map(toStringF()).subscribe(runActionSafely(View.this, new Action1<String>() {
@Override
public void call(String dateTime) {
dataSource.removeAllItems();
Object id = dataSource.addItem();
dataSource.getItem(id).getItemProperty("name").setValue(dateTime);
}
}));
clickEvent.getButton().setCaption("Stop");
}
private void cancelSubscription(Button.ClickEvent clickEvent) {
LOGGER.debug("Stopping subscription");
subscription.unsubscribe();
clickEvent.getButton().setCaption("Start");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment