Google common EventBus

Viewed 15

Google common EventBus

When I post to EventBus, nothing gets to the server.

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

    public class Server {
        private ServerSocket serverSocket;
        private Personal person;
        private EventBus eventBus;
    
        public Server(ServerSocket serverSocket) {
            this.serverSocket = serverSocket;
            this.eventBus = new EventBus();
            this.eventBus.register(this);
        }
    
        public void serverStarted() {
            try {
                Socket socket = serverSocket.accept();
                System.out.println("New Client has connected");
    
                if (socket.isConnected()) {
                    System.out.println("Connected....");
                }
       
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        @Subscribe
        public void print(Personal p) {
            this.person = p;
    
        }
    
    
        public String toString() {
            return this.person.getName();
        }
    
        public static void main(String[] args) {
            try {
                ServerSocket serverSocket = new ServerSocket(4444);
                Server server = new Server(serverSocket);
                server.serverStarted();
                System.out.println(server.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

import javax.annotation.PostConstruct;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException; 
    public class Client {
        private EventBus bus;
        public static Personal person;
    
        public Client(Socket socket) {
            try {
                this.bus = new EventBus();
                this.bus.register(this);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        @PostConstruct
        public void sendMessage(Personal personal) {
            bus.post(personal);
        }
    
        public static void main(String[] main) {
            try {
                Socket socket = new Socket("Localhost", 4444);
                Client client = new Client(socket);
                client.sendMessage(new Personal("Beka", 30, "Student"));
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

`
0 Answers
Related