is there a option to bundle different eventhandler in one javafile?
Like:
public interface MyHandlerr extends EventHandler {
void myEvent1(Event1 event);
void myEvent2(Event2 event);
}
in the moment i have for each event one handler....but i'm not happy with it.
greetz
You can create your own EventHandler interface for handling multiple events
public interface MultipleEventsHandler extends EventHandler {
void onMyEvent(MyEvent event);
void onMyOtherEvent(MyOtherEvent event);
}
Then in your event classes you can define which of the methods should be called
public class MyEvent extends GwtEvent<MultipleEventsHandler> {
public static final Type<MultipleEventsHandler> TYPE = new Type<MultipleEventsHandler>();
#Override
public Type<MultipleEventsHandler> getAssociatedType() {
return TYPE;
}
#Override
protected void dispatch(MultipleEventsHandler handler) {
handler.onMyEvent(this);
}
}
public class MyOtherEvent extends GwtEvent<MultipleEventsHandler> {
public static final Type<MultipleEventsHandler> TYPE = new Type<MultipleEventsHandler>();
#Override
public Type<MultipleEventsHandler> getAssociatedType() {
return TYPE;
}
#Override
protected void dispatch(MultipleEventsHandler handler) {
handler.onMyOtherEvent(this);
}
}
If you just want to reduce number of classes/interfaces then you can put EventHandler's inside your event classes, e.g.
public class MyEvent extends GwtEvent<MyEvent.Handler> {
public interface Handler extends EventHandler {
void onMyEvent(SomeEvent event);
}
public static final Type<MyEvent.Handler> TYPE = new Type<MyEvent.Handler>();
#Override
public Type<MyEvent.Handler> getAssociatedType() {
return TYPE;
}
#Override
protected void dispatch(MyEvent.Handler handler) {
handler.onMyOtherEvent(this);
}
}
Related
I am looking for a way to add global exception handling on all routes generated by multiple RouteTemplates. I have tried the following way but the onException block is not getting added to the routes.
Can you help me understand what I am doing wrong?
Thanks
public abstract class BaseRouteBuilder extends RouteBuilder {
#Override
public void configure(){
this.onException(IllegalStateException.class)
.log("global onException")
.maximumRedeliveries(2)
.redeliveryDelay(100)
.logStackTrace(true)
.to("direct:retryChannel");
}
}
public static class SampleRouteTemplate extends BaseRouteBuilder {
#Override
public void configure() {
super.configure();
this.routeTemplate("myTemplate")
.templateParameter("parameter1")
.from("direct:start")
.setHeader("parameter1", constant("value1"))
.log("RouteCompleted");
}
}
Try to extract a separate method for route, e.g. configureRoute() and call it in parent class in configure() method. Also make a child class non-static.
public abstract class BaseRouteBuilder extends RouteBuilder {
#Override
public final void configure(){
onException(IllegalStateException.class)
.log("global onException")
.maximumRedeliveries(2)
.redeliveryDelay(100)
.logStackTrace(true)
.to("direct:retryChannel");
configureRoute();
}
public abstract void configureRoute();
}
public class SampleRouteTemplate extends BaseRouteBuilder {
#Override
public void configureRoute() {
this.routeTemplate("myTemplate")
.templateParameter("parameter1")
.from("direct:start")
.setHeader("parameter1", constant("value1"))
.log("RouteCompleted");
}
}
}
I want to show an indeterminate ProgressBar while running the code in my Asynctask, but (if I'm right) because I'm using the .get() function in the MainActivity the UI-thread freezes until the AsyncTask gives response and thus the ProgressBar won't get displayed. How can I make it so that the ProgressBar appears on screen while the UI-thread is waiting for the Asynctask to finish and return some value?
MainActivity
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
}
public void showSpinner(View view){
CustomAsyncTask customAsyncTask = new CustomAsyncTask(this);
customAsyncTask.setProgressBar(progressBar);
try {
String message = customAsyncTask.execute().get();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Toast.makeText(this,
"Time is up", Toast.LENGTH_LONG).show();
}}
AsyncTask
public class CustomAsyncTask extends android.os.AsyncTask<Void, Void, String> {
Context context;
CustomAsyncTask(Context ctx) {
context = ctx;
}
ProgressBar progressBar;
public void setProgressBar(ProgressBar progressBar) {
this.progressBar = progressBar;
}
#Override
protected String doInBackground(Void... voids) {
SystemClock.sleep(2000);
String message = "hello world";
return message;
}
#Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
}
#Override
protected void onPostExecute(String message) {
progressBar.setVisibility(View.GONE);
}}
Try to run AsyncTask without .get().
You can use interface (see original answer):
Your interface:
public interface OnTaskCompleted{
void onTaskCompleted();
}
Your Activity:
public MainActivity extends AppCompatActivity implements OnTaskCompleted{
//your MainActivity
}
And your AsyncTask:
public class CustomAsyncTask extends android.os.AsyncTask<Void, Void, String> {
private OnTaskCompleted listener;
public CustomAsyncTask(OnTaskCompleted listener){
this.listener=listener;
}
//required methods
protected void onPostExecute(Object o){
//your stuff
listener.onTaskCompleted();
}
}
Another way is add setter for message in MainActivity:
public class MainActivity extends AppCompatActivity {
//...
private String message;
public void setMessage(String message) {
this.message = message;
}
// ...
customAsyncTask.execute();
Then just update message in .onPostExecute() in CustomAsyncTask:
public class CustomAsyncTask extends android.os.AsyncTask<Void, Void, String> {
//...
#Override
protected void onPostExecute(String message) {
progressBar.setVisibility(View.GONE);
MainActivity activity = (MainActivity) context;
activity.setMessage(message);
}
I have widget. I would like to fire an event as follow:
fireEvent(new IndicatorStartEvent("Message"));
But it dosn't work.
Normally I use Presenter for this (GWTP), but now I just would like to have regular widget:
public class FileUploadWidget extends Composite {
MaterialFileUploader uploader = new MaterialFileUploader();
#Inject
public FileUploadWidget(String triggerId, EventBus eventBus) {
super();
initWidget(uploader);
Window.alert("TEST Start");
fireEvent(new IndicatorStartEvent("Message"));
}
}
Here is event code:
public class IndicatorStartEvent extends GwtEvent<IndicatorStartEvent.IndicatorHandler> {
public static Type<IndicatorHandler> TYPE = new Type<IndicatorHandler>();
public interface IndicatorHandler extends EventHandler {
void onIndicatorProgressStart(IndicatorStartEvent event);
}
public interface IndicatorHandlers extends HasHandlers {
HandlerRegistration addStartIndicatorHandler(IndicatorHandler handler);
}
private final String message;
public IndicatorStartEvent(final String message) {
this.message = message;
}
public static Type<IndicatorHandler> getType() {
return TYPE;
}
#Override
protected void dispatch(final IndicatorHandler handler) {
handler.onIndicatorProgressStart(this);
}
#Override
public Type<IndicatorHandler> getAssociatedType() {
return TYPE;
}
public String getMessage() {
return this.message;
}
}
This is my app presenter that handle the event:
public class AppPresenter extends TabContainerPresenter<AppPresenter.MyView, AppPresenter.MyProxy> implements AppUiHandlers
, IndicatorStartEvent.IndicatorHandler {
#ProxyStandard
public interface MyProxy extends Proxy<AppPresenter> {}
public interface MyView extends TabView, HasUiHandlers<AppUiHandlers> {}
#Override
protected void onBind() {
super.onBind();
addRegisteredHandler(IndicatorStartEvent.getType(), this);
}
public void onAsyncCallFail(AsyncCallFailEvent event) {
// fireEvent is executed from: com.gwtplatform.mvp.client;PresenterWidget
fireEvent(new IndicatorStartEvent("Firing message"));
}
#Override
public void onIndicatorProgressStart(IndicatorStartEvent event) {
MaterialToast.fireToast("Indicator start: " + event.getMessage());
}
}
If I fire this event from f.e.: AppPresenter (code above), or GwtRESTY filter/callback ass follow:
class ProgressIndicatorFilter implements DispatcherFilter {
private AssistedInjectionFactory factory;
private EventBus eventBus;
#Inject
public ProgressIndicatorFilter(AssistedInjectionFactory factory, EventBus eventBus) {
this.factory = factory;
this.eventBus = eventBus;
}
#Override
public boolean filter(Method method, RequestBuilder builder) {
builder.setCallback(factory.createProgressIndicatorCallback(method));
eventBus.fireEvent(new IndicatorStartEvent("Rest-Gwt Comunication started"));
return true;
}
}
It work as expected. But in those working examples it use com.google.web.bindery.event.shared;EventBus
The firing event doesnt work from widget, where is used:
com.google.gwt.event.shared;HandlerManager;Bus class. This class Bus extends com.google.web.bindery.event.shared.SimpleEventBus which extends the proper EventBus class from com.google.web.bindery.event.shared;EventBus.
So the widget's method fireEvent() use other EventBus.
Can anyone help me with this?
I've red official and this instruction:
http://blog.arcbees.com/2015/04/01/gwt-platform-event-best-practices-revisited/ but no luck so far. Please help.
It does not work because your FileUploadWidget uses it's own EventBus and not GWTP one that is also used in all of your Presenters.
There are two solutions:
Don't use fireEvent(new IndicatorStartEvent("Message")) but use eventBus.fireEvent(new IndicatorStartEvent("Message")) on the injected EventBus inside of your Widget.
Add the IndicatorStartEvent handler to your FileUploadWidget directly instead of using addRegisteredHandler on your Presenter.
I prefer solution 2:
public class FileUploadWidget extends Composite {
MaterialFileUploader uploader = new MaterialFileUploader();
#Inject
public FileUploadWidget(String triggerId) {
super();
initWidget(uploader);
Window.alert("TEST Start");
fireEvent(new IndicatorStartEvent("Message"));
}
}
In the Presenter or to be precise the View which uses your FileUploadWidget, you add a handler directly to the FileUploadWidget:
public class UploadView extends ViewWithUiHandlers<UploadUiHandlers> implements UploadPresenter.MyView,IndicatorStartEvent.IndicatorHandler {
#UiField
FileUploadWidget uploadWidget;
#Inject
public UploadView(final Binder binder) {
widget = binder.createAndBindUi(this);
uploadWidget.addHandler(new IndicatorStartEvent.Handler(),this);
}
public void onIndicatorProgressStart(IndicatorStartEvent event) {
MaterialToast.fireToast("Indicator start: " + event.getMessage());
}
}
I am working on developing a JMeter plugin. I'm trying to create an AbstractVisualizer that is capable of monitoring the current test state. However, implementing the TestStateListener doesn't seem to be working.
I'm testing this by creating a basic listener that has a login to output arbitrary info to JMeter's logging console. When a sample is sent through the Add function, a line is sent to the console. But nothing is ever triggered on the various TestState functions. Is there something more structural I'm missing?
public class TestListener extends AbstractVisualizer
implements TestStateListener
{
private static final Logger log = LoggingManager.getLoggerForClass();
#Override
public void add(SampleResult arg0) {
log.info("add");
}
#Override
public void clearData() {
// TODO Auto-generated method stub
}
#Override
public String getStaticLabel()
{
return "Test Listener";
}
#Override
public String getLabelResource() {
return null;
}
#Override
public void testEnded() {
log.info("Test Ended");
}
#Override
public void testEnded(String arg0) {
log.info("Test Ended");
}
#Override
public void testStarted() {
log.info("Test started");
}
#Override
public void testStarted(String arg0) {
log.info("Test started");
}
}
I'm not sure how to do it in 1 class. I have 2 classes:
The UI:
public class MonitorGui extends AbstractListenerGui
{
// ...
#Override
public TestElement createTestElement()
{
TestElement element = new Monitor();// <-- this is the backend
modifyTestElement(element);
return element;
}
// ...
}
And then the backend goes like this:
public class Monitor extends AbstractListenerElement
implements SampleListener,
Clearable, Serializable,
TestStateListener, Remoteable,
NoThreadClone
{
private static final String TEST_IS_LOCAL = "*local*";
// ...
#Override
public void testStarted()
{
testStarted(TEST_IS_LOCAL);
}
#Override
public void testEnded()
{
testEnded(TEST_IS_LOCAL);
}
#Override
public void testStarted(String host)
{
// ...
}
// ...
}
You may not need to implement SampleListener like I do, but probably other things are quite similar.
I based that implementation on a built-in pair of ResultSaverGui and ResultCollector which are the components that are saving results into the file(s) for Simple Data Writer, Summary Report and so on.
I'm just new in the spring architecture and I am wondering if it is possible to use a generic T (o what you want) in an ItemStreamReader.
Something like this:
public class Reader implements ItemStreamReader<T extends SomeClass>{
public T read() {
.......
}
public void open() {
.......
}
public void update() {
.......
}
public void close() {
.......
}
}
So I pass to the reader various objects that extends SomeClass.
this should work:
public class Reader<T extends SomeClass> implements ItemStreamReader<T>{
public T read() {
.......
}
public void open() {
.......
}
public void update() {
.......
}
public void close() {
.......
}
}
use it like:
Reader<SomeClass> reader = new Reader<>();
Reader<ExtendedFromSomeClass> reader2 = new Reader<>();
I recommend you to read first about java generics.
http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#What is a parameterized (or generic) type?
But if you define your class
public class Reader implements ItemStreamReader<SomeClass>{
public SomeClass read() {
.......
}
public void open() {
.......
}
public void update() {
.......
}
public void close() {
.......
}
}
Your method can return any object that is Subclass of SomeClass.
For example
Reader a = new Reader();
Subclass b = (Subclass)a.read();