Problem with guava EventBus

package com.example.vaadin7scratch;
 
import java.io.*;
import java.net.*;
 
import javax.servlet.annotation.WebServlet;
 
import com.google.gwt.thirdparty.guava.common.eventbus.AllowConcurrentEvents;
import com.google.gwt.thirdparty.guava.common.eventbus.Subscribe;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
 
import dispatching.EventBroadcastEvent;
import dispatching.EventBusSingleton;
 
@SuppressWarnings("serial")
@Theme("vaadin7scratch")
public class Vaadin7scratchUI extends UI {
 
Socket client = null;
DataOutputStream out = null;
BufferedWriter bw = null;
 
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = Vaadin7scratchUI.class)
public static class Servlet extends VaadinServlet {
}
 
 
@Subscribe
@AllowConcurrentEvents
private void readIncomingMessages(EventBroadcastEvent e){
Notification.show("Message containing: "+e.getName());
 
}
 
 
@Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
 
EventBusSingleton.getInstance().register(this);
write("This is message is written as soon as the user beholds the newly created UI."+"\n");
 
 
Button button = new Button("Click Me");
button.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
 
write("New Message");
layout.addComponent(new Label("Thank you for clicking and Your new message"));
 
}
});
layout.addComponent(button);
}
 
private void write(String towrite){
 
 
try {
client = new Socket("127.0.0.1", 8754);
out = new DataOutputStream(client.getOutputStream());
bw = new BufferedWriter(new OutputStreamWriter(out));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
 
try {
 
bw.write(towrite);
bw.flush();
//bw.close();
 
}catch (IOException e) {
 
e.printStackTrace();
 
}
}
 
}

At the moment, I am writing a chat application for autodidactic purposes. I have created a Server socket which is supposed to broadcast an event after receiving a message by a stream, and I want to catch this event and show it by a Notification. The VaadinGUI should simply send a String “New Message” to the server, and the server should broadcast an event which I want to catch with an event subscriber method (see above).

Now this is my server:

[code]
package sockets;

import java.net.;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.io.
;

import com.google.gwt.thirdparty.guava.common.eventbus.EventBus;
import com.vaadin.ui.Notification;

import dispatching.EventBroadcastEvent;
import dispatching.EventBusSingleton;

public class Server extends Thread
{
private ServerSocket serverSocket;
private HashMap<String,Socket> dispatch = new HashMap<String,Socket>();
private BufferedReader br = null;
private BufferedWriter bw = null;

public Server(int port) throws IOException
{
serverSocket = new ServerSocket(port);
//serverSocket.setSoTimeout(10000);
}

public void run()
{

while(true)
{
try
{

Socket server = serverSocket.accept();

try{

br = new BufferedReader(new InputStreamReader(new DataInputStream(server.getInputStream())));

String message = br.readLine();

EventBusSingleton.getInstance().post(new EventBroadcastEvent(message));

}catch(IOException e){e.printStackTrace();}

}catch(SocketTimeoutException s)
{
System.out.println(“Socket timed out!”);
break;
}catch(IOException e)
{
e.printStackTrace();
break;
}
}

}

public static void main(String args)
{

try
{
Thread t = new Server(8754);
t.start();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
[/code]I create the EventBus with a singleton pattern - as follows:

[code]
package dispatching;

import com.google.gwt.thirdparty.guava.common.eventbus.EventBus;

public class EventBusSingleton {

private static EventBus eventBus = null;

private EventBusSingleton() {}

public static EventBus getInstance() {
if (eventBus == null) {
eventBus = new EventBus();
}
return eventBus;
}

}
[/code]Unfortunately, no Notification whatsoever is shown; but I cannot figure out what the mistake is.

Your @Subscribe methods need to be public.

Well, now I have overhauled everythin - the event dispatching does not seem to be working, yet.

I register the Eventbus in the constructor: EventBusFactory.getEventBusInstance().register(this);

Then, I have a method, that subscribes to the eventbus:

@Subscribe
public void addLogOnEvent(EventLogOnEvent event){
Notification.show(“Hier sollen dann die Onlinestatus der Buddies refreshed werden.”);
}

I have written an Event-Object which is posted in the code (I checked up on that) - but somehow nothing happens. My Eventobject looks like this:

[code]
package dispatch;

public class EventLogOnEvent {

}
[/code]I do not know where the mistake might be.

 package com.tsher;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;

import com.google.gwt.thirdparty.guava.common.eventbus.Subscribe;
import com.tsher.Resources.Imageresource;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.server.ClientConnector;
import com.vaadin.server.FileResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Image;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;

import daacc.BuddiesDAOImpl;
import daacc.LgnAccDAOImpl;
import dispatch.EventBroadcastEvent;
import dispatch.EventBusFactory;
import dispatch.EventLogOnEvent;

/**
* @author sleipnir
*
*/
public class ChatView extends CustomComponent implements View{



private static final long serialVersionUID = 1L;

public static final String NAME = "chatview";

private LgnAccDAOImpl lgn = null;
private Button lgoutaction = null;

/**
* Konstruktor
*/
public ChatView() {
EventBusFactory.getEventBusInstance().register(this);
createTools();
setCompositionRoot(createChatLayout());

}

@Subscribe
public void addLogOnEvent(EventLogOnEvent event){
Notification.show("Hier sollen dann die Onlinestatus der Buddies refreshed werden.");
}



/**
* Es werden die nötogen Datenstruktiren initialisiert.
*/
private void createTools(){

//DACC
lgn = new LgnAccDAOImpl();

//Logoutbutton
lgoutaction = new Button("logout");
lgoutaction.setWidth(180, Unit.PIXELS);
lgoutaction.addClickListener(new Button.ClickListener() {
private static final long serialVersionUID = 1L;

public void buttonClick(ClickEvent event) {

LgnAccDAOImpl lgn = new LgnAccDAOImpl();
String username = (String) UI.getCurrent().getSession().getAttribute("currentUser");
lgn.setOnlineStat(username,UI.getCurrent(),false);

logOnAndOff();

}
});

}



@SuppressWarnings({ "deprecation", "unchecked" })
private GridLayout createChatLayout(){

final GridLayout gridlayout = new GridLayout(3, 3);
gridlayout.addStyleName(ThemeStyle.Chatgridlayout);
gridlayout.setMargin(true);

gridlayout.addComponent(lgoutaction,0,0);


return gridlayout;
}



private void logOnAndOff(){

EventLogOnEvent eln = new EventLogOnEvent();
EventBusFactory.getEventBusInstance().post(eln);
}


@Override
public void enter(ViewChangeEvent event) {

String username = (String) UI.getCurrent().getSession().getAttribute("currentUser");
lgn.setOnlineStat(username,UI.getCurrent(),true);

//Wenn sich der Onlinestatus geändert hat, dann wird die Methode logOnAndOff aufgerufen.
logOnAndOff();

}

}

Hi,

Don’t know if you’re still having the issue but I believe it is due to you using the wrong version of EventBus

i.e. You have:

import com.google.gwt.thirdparty.guava.common.eventbus.AllowConcurrentEvents; import com.google.gwt.thirdparty.guava.common.eventbus.Subscribe; and instead you should have:

import com.google.common.eventbus.Subscribe;
import com.google.common.eventbus.AllowConcurrentEvents;

Blame Eclipse/NetBeans autofix includes.
I just lost an afternoon to the same issue… ba!