praktikum3
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
<server xmlns="urn:jboss:domain:community:20.0">
|
<server xmlns="urn:jboss:domain:community:20.0">
|
||||||
<extensions>
|
<extensions>
|
||||||
<extension module="org.jboss.as.clustering.infinispan"/>
|
<extension module="org.jboss.as.clustering.infinispan"/>
|
||||||
@@ -452,7 +451,7 @@
|
|||||||
<smtp-server outbound-socket-binding-ref="mail-smtp"/>
|
<smtp-server outbound-socket-binding-ref="mail-smtp"/>
|
||||||
</mail-session>
|
</mail-session>
|
||||||
<mail-session name="uebung3" debug="false" jndi-name="java:/jboss/mail/uebung3">
|
<mail-session name="uebung3" debug="false" jndi-name="java:/jboss/mail/uebung3">
|
||||||
<smtp-server outbound-socket-binding-ref="mail-smtp-gmail" ssl="false" tls="true" username="<invalid>" password="<invalid>"/>
|
<smtp-server outbound-socket-binding-ref="mail-smtp-gmail" ssl="false" tls="true" username="invalid" password="invalid"/>
|
||||||
</mail-session>
|
</mail-session>
|
||||||
</subsystem>
|
</subsystem>
|
||||||
<subsystem xmlns="urn:jboss:domain:messaging-activemq:17.0">
|
<subsystem xmlns="urn:jboss:domain:messaging-activemq:17.0">
|
||||||
@@ -484,6 +483,7 @@
|
|||||||
<connection-factory name="InVmConnectionFactory" entries="java:/ConnectionFactory" connectors="in-vm"/>
|
<connection-factory name="InVmConnectionFactory" entries="java:/ConnectionFactory" connectors="in-vm"/>
|
||||||
<connection-factory name="RemoteConnectionFactory" entries="java:jboss/exported/jms/RemoteConnectionFactory" connectors="http-connector"/>
|
<connection-factory name="RemoteConnectionFactory" entries="java:jboss/exported/jms/RemoteConnectionFactory" connectors="http-connector"/>
|
||||||
<pooled-connection-factory name="activemq-ra" entries="java:/JmsXA java:jboss/DefaultJMSConnectionFactory" connectors="in-vm" transaction="xa"/>
|
<pooled-connection-factory name="activemq-ra" entries="java:/JmsXA java:jboss/DefaultJMSConnectionFactory" connectors="in-vm" transaction="xa"/>
|
||||||
|
<jms-topic name="Praktikum3Topic" entries="java:/jms/topic/Praktikum3Topic java:jboss/exported/jms/topic/Praktikum3Topic"/>
|
||||||
</server>
|
</server>
|
||||||
</subsystem>
|
</subsystem>
|
||||||
<subsystem xmlns="urn:wildfly:metrics:1.0" security-enabled="false" exposed-subsystems="*" prefix="${wildfly.metrics.prefix:wildfly}"/>
|
<subsystem xmlns="urn:wildfly:metrics:1.0" security-enabled="false" exposed-subsystems="*" prefix="${wildfly.metrics.prefix:wildfly}"/>
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package org.example.demo.praktikum3;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.FlowLayout;
|
||||||
|
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JTextArea;
|
||||||
|
import javax.swing.JTextField;
|
||||||
|
|
||||||
|
public class ChatWindow extends JFrame {
|
||||||
|
private JTextArea textArea;
|
||||||
|
private MessageReceiver receiver;
|
||||||
|
|
||||||
|
private JPanel inputPanel;
|
||||||
|
private JTextField messageInput;
|
||||||
|
private JButton messageSubmit;
|
||||||
|
private MessageSender sender;
|
||||||
|
|
||||||
|
public ChatWindow(MessageReceiver receiver, MessageSender sender) {
|
||||||
|
textArea = new JTextArea();
|
||||||
|
this.add(BorderLayout.NORTH, textArea);
|
||||||
|
receiver.setText(textArea);
|
||||||
|
|
||||||
|
inputPanel = new JPanel();
|
||||||
|
inputPanel.setLayout(new FlowLayout());
|
||||||
|
this.add(BorderLayout.SOUTH, inputPanel);
|
||||||
|
|
||||||
|
messageInput = new JTextField(20);
|
||||||
|
inputPanel.add(messageInput);
|
||||||
|
|
||||||
|
messageSubmit = new JButton("Submit");
|
||||||
|
messageSubmit.addActionListener(e -> {
|
||||||
|
sender.sendMessage(messageInput.getText());
|
||||||
|
});
|
||||||
|
inputPanel.add(messageSubmit);
|
||||||
|
|
||||||
|
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package org.example.demo.praktikum3;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
import javax.naming.Context;
|
||||||
|
import javax.naming.InitialContext;
|
||||||
|
import javax.naming.NamingException;
|
||||||
|
import jakarta.jms.ConnectionFactory;
|
||||||
|
import jakarta.jms.Topic;
|
||||||
|
|
||||||
|
public class Client {
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
public Client(int id) {
|
||||||
|
this.id = id;
|
||||||
|
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
|
||||||
|
props.setProperty(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080");
|
||||||
|
|
||||||
|
try {
|
||||||
|
InitialContext context = new InitialContext(props);
|
||||||
|
ConnectionFactory cf = (ConnectionFactory) context.lookup("jms/RemoteConnectionFactory");
|
||||||
|
Topic topic = (Topic) context.lookup("jms/topic/Praktikum3Topic");
|
||||||
|
|
||||||
|
var sender = new MessageSender(topic, cf, "Sender " + this.id + ": ");
|
||||||
|
var receiver = new MessageReceiver(topic, cf, null);
|
||||||
|
var win = new ChatWindow(receiver, sender);
|
||||||
|
|
||||||
|
win.setVisible(true);
|
||||||
|
win.setTitle("Chat - Sender " + this.id);
|
||||||
|
receiver.start();
|
||||||
|
} catch (NamingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package org.example.demo.praktikum3;
|
||||||
|
|
||||||
|
public class Client1 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new Client(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package org.example.demo.praktikum3;
|
||||||
|
|
||||||
|
public class Client2 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new Client(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package org.example.demo.praktikum3;
|
||||||
|
|
||||||
|
import javax.swing.JTextArea;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
|
import jakarta.jms.ConnectionFactory;
|
||||||
|
import jakarta.jms.JMSConsumer;
|
||||||
|
import jakarta.jms.JMSContext;
|
||||||
|
import jakarta.jms.Topic;
|
||||||
|
|
||||||
|
public class MessageReceiver extends Thread {
|
||||||
|
private JTextArea text;
|
||||||
|
private Topic topic;
|
||||||
|
private ConnectionFactory cf;
|
||||||
|
|
||||||
|
public MessageReceiver(Topic topic, ConnectionFactory cf, JTextArea text) {
|
||||||
|
this.text = text;
|
||||||
|
this.topic = topic;
|
||||||
|
this.cf = cf;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JTextArea getText() {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setText(JTextArea text) {
|
||||||
|
this.text = text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
try (JMSContext context = cf.createContext("guest", "guest")) {
|
||||||
|
JMSConsumer consumer = context.createConsumer(topic);
|
||||||
|
while (true) {
|
||||||
|
String message = consumer.receiveBody(String.class);
|
||||||
|
if (message != null) {
|
||||||
|
SwingUtilities.invokeLater(() -> text.append(message + "\n"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package org.example.demo.praktikum3;
|
||||||
|
|
||||||
|
import jakarta.jms.ConnectionFactory;
|
||||||
|
import jakarta.jms.JMSContext;
|
||||||
|
import jakarta.jms.JMSProducer;
|
||||||
|
import jakarta.jms.TextMessage;
|
||||||
|
import jakarta.jms.Topic;
|
||||||
|
|
||||||
|
public class MessageSender {
|
||||||
|
private String prefix;
|
||||||
|
private Topic topic;
|
||||||
|
private ConnectionFactory cf;
|
||||||
|
|
||||||
|
public MessageSender(Topic topic, ConnectionFactory cf, String prefix) {
|
||||||
|
this.topic = topic;
|
||||||
|
this.cf = cf;
|
||||||
|
this.prefix = prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendMessage(String msg) {
|
||||||
|
var text = prefix + msg;
|
||||||
|
|
||||||
|
try (JMSContext context = cf.createContext("guest", "guest")) {
|
||||||
|
JMSProducer producer = context.createProducer();
|
||||||
|
TextMessage message = context.createTextMessage(text);
|
||||||
|
producer.send(topic, message);
|
||||||
|
System.out.println("Message '"+text+"' sent successfully.");
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
System.out.println("Message '"+text+"' couldn't be sent: " + e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user