Skip to content

Instantly share code, notes, and snippets.

@anujku
Created December 14, 2014 22:57
Show Gist options
  • Save anujku/5e5e723ad86c0c176ab4 to your computer and use it in GitHub Desktop.
Save anujku/5e5e723ad86c0c176ab4 to your computer and use it in GitHub Desktop.
Command line chat using Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class CommandLineChat implements MessageListener {
public static void main(String[] args) throws JMSException, NamingException, IOException{
if(args.length != 3)
System.out.println("usage : username subscribe-to-queue-name publish-to-queue-name");
else
{
String userName = args[0];
String queryName01 = "queue/" + args[1];
String queryName02 = "queue/" + args[2];
CommandLineChat chat = new CommandLineChat();
Context initialContext = CommandLineChat.getInitialContext();
Queue queue01 = (Queue)initialContext.lookup(queryName01);
Queue queue02 = (Queue)initialContext.lookup(queryName02);
QueueConnectionFactory connectionFactory = (QueueConnectionFactory) initialContext.lookup("ConnectionFactory");
QueueConnection connection = connectionFactory.createQueueConnection();
chat.subscribe(connection, queue01, chat);
chat.publish(connection, queue02, chat, userName);
}
}
@Override
public void onMessage(Message msg) {
try {
System.out.println(((TextMessage) msg).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
public void subscribe(QueueConnection queueConnection, Queue queue, CommandLineChat commandLineChat) throws JMSException{
QueueSession subscribeSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueReceiver queueReceiver = subscribeSession.createReceiver(queue);
queueReceiver.setMessageListener(commandLineChat);
}
public void publish(QueueConnection queueConnection, Queue queue, CommandLineChat commandLineChat, String userName) throws JMSException, IOException{
QueueSession publishSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueSender queueSender = publishSession.createSender(queue);
queueConnection.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true){
String messageToSend = reader.readLine();
if(messageToSend.equalsIgnoreCase("Exit")) {
queueConnection.close();
System.exit(0);
}
else
{
TextMessage message = publishSession.createTextMessage();
message.setText("[" + userName+ ": "+ messageToSend + " ]");
queueSender.send(message);
}
}
}
public static Context getInitialContext() throws JMSException, NamingException{
Properties properties = new Properties();
properties.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
properties.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
properties.setProperty("java.naming.factory.provider.url", "localhost:1099");
Context context = new InitialContext(properties);
return context;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment