Showing posts with label Ajax. Show all posts
Showing posts with label Ajax. Show all posts

Tuesday, January 1, 2008

Java becoming old?

Some "experts" claiming that Java becoming old and that it would reach it's end in a few years,
Some of these "experts" claim that java would end as Cobol, and new technologies would be adopted such as "Ruby On Rails", PHP and Ajax.

I completely disagree with this statements, First of Cobol is still one of the most common software programming languages. Beside that other new technologies such as Ruby still doesn't mature - we've tried several projects with it and have reverted to Java.
PHP is cool, but doesn't scale as well as Java for large user base applications.
AJAX -is like Javascript and CSS; It intended to be used with JSP/ASP/HTML etc.
Java still is the best choice - by far - for development. It runs on virtually any platform and doesn't lock you in to one OS vendor or flavor. It works, and it's fast. It's a great tool for back-end and web development. Yes, it's not the newest kid on the block, and frankly that doesn't matter much - It has solid performance and results pretty well in production

Tuesday, December 11, 2007

Free Chat Implementation in GWT

I want to share my chat implementation for GWT, this is a pretty simple code which demonstrate how to write an Instand Messaging system inside the browser using AJAX and GWT technology.

The idea is simple, we have a chat factory in which each client can subscribe, There is a Chat Panel in which the user can send messages to the sever in async way. In the server there is a List which holds all the messages which all the clients sent.

There is a polling mechanism which run every few seconds and check if there are new chat messages to display.

The implementation is not state of the art but it works and it can be improved to work better.

Enjoy the implementation:


public class MyChatFactory {

private static final MyChatFactory instance = new MyChatFactory();
private static final int DELAY = 10000;

private MyChatFactory() {
}

public static MyChatFactory getInstance() {
return instance;
}

public void subscribeToEvent(final String queueName, final ChatCallback callback) {
ClientUtil.log("start subscribeToEvent");

Timer eventsTimer = new Timer() {
public void run() {
ChatProxy.getEvents(queueName, callback);
schedule(DELAY);
}
};

eventsTimer.schedule(DELAY);
}

public void sendMessage(String queueName, ChatMessage message) {
ChatProxy.sendMessage(queueName, message);
}

public void sendMessageToAll(ChatMessage message) {
ChatProxy.sendMessage(null, message);
}
}

public class ChatCallback extends GeneralCallback {

public ChatCallback() {
}

public void onSuccess(Object result) {
//ClientUtil.log("start ChatCallback.onSuccess");
if (result!=null) {
ChatMessage chatMessage = (ChatMessage) result;

if (chatMessage.isLogin()) {
ChatPanel.getInstance().addUser(chatMessage.getMessage());
} else {
ChatPanel.getInstance().addText(chatMessage.getMessage());
}
}
//ClientUtil.log("finish ChatCallback.onSuccess");
}

public void onFailure(Throwable caught) {
ClientUtil.log(caught.getMessage());
}
}

public class ChatMessage implements IsSerializable{

private String message;
private boolean isLogin;

public ChatMessage() {
}

public ChatMessage(String message, boolean isLogin) {
this.message=message;
this.isLogin=isLogin;
}


public boolean isLogin() {
return isLogin;
}
public void setLogin(boolean isLogin) {
this.isLogin = isLogin;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

public class ChatProxy {

private static ChatServiceAsync chatService = (ChatServiceAsync) GWT.create(ChatService.class);

static {
ClientUtil.setEndpointForService(chatService, "chat");
}

public static void getEvents(String queueName, ChatCallback callback) {
chatService.getEvents(queueName, callback);

}

public static void sendMessage(String queueName, ChatMessage message) {
VoidCallback callback = new VoidCallback();
chatService.sendMessage(queueName, message, callback);
}

}

public interface ChatService extends RemoteService {

public ChatMessage getEvents(String queueName);

public void sendMessage(String queueName, ChatMessage chatMessage);
}

public class ChatServiceImpl extends RemoteServiceServlet implements ChatService {

private static final long serialVersionUID = 1L;

private static Map subscribers = new HashMap();
//private static final long INTERVAL = 200000-1000;

public ChatMessage getEvents(String queueName) {
List list = null;

if (!subscribers.containsKey(queueName)) {
list = new ArrayList();
subscribers.put(queueName, list);
} else {
list = (List) subscribers.get(queueName);
}

if (!list.isEmpty()) {
ChatMessage message = (ChatMessage) list.get(0);
list.remove(0);
return message;
}

return null;
}

public void sendMessage(String queueName, ChatMessage chatMessage) {

if (queueName!=null) {
if (subscribers.containsKey(queueName)) {
List list = (List) subscribers.get(queueName);
list.add(chatMessage);
}
} else { // If null then send to all
Iterator subscribeIter = subscribers.values().iterator();

while (subscribeIter.hasNext()) {
List list = (List) subscribeIter.next();
list.add(chatMessage);
// list.notify();
}
}
}

}

Links

 
RSS Feeds Submission Directory