Finished chat
parent
9bc0e44ef6
commit
91d6b78f5b
Binary file not shown.
Binary file not shown.
|
|
@ -1,30 +1,27 @@
|
||||||
package var.mom.jms.chat;
|
package var.mom.jms.chat;
|
||||||
|
|
||||||
import javax.jms.Connection;
|
import javax.jms.*;
|
||||||
import javax.jms.ConnectionFactory;
|
|
||||||
import javax.jms.Destination;
|
|
||||||
import javax.jms.JMSException;
|
|
||||||
import javax.jms.Message;
|
|
||||||
import javax.jms.MessageConsumer;
|
|
||||||
import javax.jms.MessageListener;
|
|
||||||
import javax.jms.Session;
|
|
||||||
import javax.jms.TextMessage;
|
|
||||||
import javax.naming.Context;
|
import javax.naming.Context;
|
||||||
import javax.naming.InitialContext;
|
import javax.naming.InitialContext;
|
||||||
import javax.naming.NamingException;
|
import javax.naming.NamingException;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
// Code aus JMS-Client mit Umbenennung des Typs von JMSClient zu ChatClient und Anpassung des packages
|
// Code aus JMS-Client mit Umbenennung des Typs von JMSClient zu ChatClient und Anpassung des packages
|
||||||
public class ChatClient implements MessageListener {
|
public class ChatClient implements MessageListener {
|
||||||
private Connection connection;
|
private Connection connection;
|
||||||
private Session session;
|
private Session session;
|
||||||
private MessageConsumer consumer;
|
private MessageConsumer consumer;
|
||||||
|
private MessageProducer producer;
|
||||||
|
|
||||||
public ChatClient() throws NamingException, JMSException {
|
public ChatClient() throws NamingException, JMSException {
|
||||||
Context ctx = new InitialContext();
|
Context ctx = new InitialContext();
|
||||||
ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
|
ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
|
||||||
Destination queue = (Destination) ctx.lookup("var.mom.jms.chat.queue");
|
Destination queue = (Destination) ctx.lookup(Conf.QUEUE);
|
||||||
connection = factory.createConnection();
|
connection = factory.createConnection();
|
||||||
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||||
|
|
||||||
|
producer = session.createProducer(queue);
|
||||||
|
|
||||||
consumer = session.createConsumer(queue);
|
consumer = session.createConsumer(queue);
|
||||||
consumer.setMessageListener(this);
|
consumer.setMessageListener(this);
|
||||||
connection.start();
|
connection.start();
|
||||||
|
|
@ -36,22 +33,39 @@ public class ChatClient implements MessageListener {
|
||||||
if (message instanceof TextMessage) {
|
if (message instanceof TextMessage) {
|
||||||
TextMessage textMessage = (TextMessage) message;
|
TextMessage textMessage = (TextMessage) message;
|
||||||
String messageText = textMessage.getText();
|
String messageText = textMessage.getText();
|
||||||
String priority = textMessage.getStringProperty("Priority");
|
String userName = textMessage.getStringProperty("Name");
|
||||||
System.out.println(messageText + " [Priority=" + priority + "]");
|
System.out.println(userName + ": " + messageText);
|
||||||
}
|
}
|
||||||
} catch (JMSException e) {
|
} catch (JMSException e) {
|
||||||
System.err.println(e);
|
System.err.println(e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendMessage(String messageText, String userName) throws JMSException {
|
||||||
|
TextMessage textMessage = session.createTextMessage();
|
||||||
|
|
||||||
|
textMessage.setText(messageText);
|
||||||
|
textMessage.setStringProperty("Name", userName);
|
||||||
|
|
||||||
|
producer.send(textMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
long wait = Long.parseLong(args[0]);
|
//long wait = Long.parseLong(args[0]);
|
||||||
ChatClient node = null;
|
ChatClient node = null;
|
||||||
|
String userName = "Nastja";
|
||||||
try {
|
try {
|
||||||
node = new ChatClient();
|
node = new ChatClient();
|
||||||
Thread.sleep(wait);
|
|
||||||
} catch (InterruptedException | NamingException | JMSException e) {
|
while(true) {
|
||||||
|
Scanner keyboard = new Scanner(System.in);
|
||||||
|
//System.out.println("> ");
|
||||||
|
String messageText = keyboard.nextLine();
|
||||||
|
System.out.println("");
|
||||||
|
node.sendMessage(messageText, userName);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch ( NamingException | JMSException e) {
|
||||||
System.err.println(e);
|
System.err.println(e);
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
package var.mom.jms.chat;
|
||||||
|
|
||||||
|
public class Conf {
|
||||||
|
public static final String QUEUE = "var.mom.jms.channel1";
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue