Created
January 7, 2019 13:17
-
-
Save dferrandizmont/94f08d07c9af82eea9b06788b78dc68a to your computer and use it in GitHub Desktop.
[ReactiveBean] JavaServer Faces with rxJava. #rxjava
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.reactive; | |
import java.io.Serializable; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.enterprise.context.RequestScoped; | |
import javax.inject.Named; | |
import rx.Observable; | |
import rx.Subscriber; | |
@Named | |
@RequestScoped | |
public class ReactiveBean implements Serializable { | |
private Question question; | |
private String answer; | |
public void doReactive() { | |
myObservable.subscribe(mySubscriber); | |
} | |
public ReactiveBean() { | |
question = new Question(); | |
} | |
public List<Question> getQuestionList() { | |
List<Question> list = new ArrayList<>(); | |
Question q1 = new Question(); | |
q1.setId(1L); | |
q1.setQuestion("What is the best development IDE?"); | |
list.add(q1); | |
Question q2 = new Question(); | |
q2.setId(2L); | |
q2.setQuestion("What is the best programming language?"); | |
list.add(q2); | |
Question q3 = new Question(); | |
q3.setId(3L); | |
q3.setQuestion("What is the best country in the World?"); | |
list.add(q3); | |
return list; | |
} | |
@SuppressWarnings("deprecation") | |
Observable<String> myObservable = Observable.create( | |
new Observable.OnSubscribe<String>() { | |
@Override | |
public void call(Subscriber<? super String> sub) { | |
if (question.getId().equals(1L)) { | |
sub.onNext("NetBeans"); | |
sub.onCompleted(); | |
} | |
if (question.getId().equals(2L)) { | |
sub.onNext("Java"); | |
sub.onCompleted(); | |
} | |
if (question.getId().equals(3L)) { | |
sub.onNext("Armenia"); | |
sub.onCompleted(); | |
} | |
} | |
} | |
); | |
private final Subscriber<String> mySubscriber = new Subscriber<String>() { | |
@Override | |
public void onNext(String s) { | |
answer = s; | |
System.out.println("Subscriber " + s); | |
} | |
@Override | |
public void onCompleted() { | |
} | |
@Override | |
public void onError(Throwable e) { | |
} | |
}; | |
////////////////////////////////////////////// | |
public static void hello(String... names) { | |
Observable.from(names).subscribe((String s) -> { | |
System.out.println("Hello " + s + "!"); | |
}); | |
} | |
public String getAnswer() { | |
return answer; | |
} | |
public void setAnswer(String answer) { | |
this.answer = answer; | |
} | |
public Question getQuestion() { | |
return question; | |
} | |
public void setQuestion(Question question) { | |
this.question = question; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment