package org.example.demo.uebung3.aufgabe13;

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import jakarta.jms.Connection;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.Destination;
import jakarta.jms.JMSException;
import jakarta.jms.MessageProducer;
import jakarta.jms.Session;
import jakarta.jms.TextMessage;

public class Client {

    public static void main(String[] args) {
        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 und Ziel-Queue ermitteln
            ConnectionFactory factory = (ConnectionFactory) context.lookup("jms/RemoteConnectionFactory");
            Destination queue = (Destination) context.lookup("jms/queue/MyQueue");

            // Verbindung und Session aufbauen
            Connection con = factory.createConnection("guest", "guest");
            Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
            con.start();

            // Nachricht erstellen und senden
            MessageProducer producer = session.createProducer(queue);
            TextMessage message = session.createTextMessage("Hallo-Welt");
            producer.send(message);

            System.out.println("Nachricht 'Hallo-Welt' in Queue gestellt.");

            // Ressourcen schließen
            producer.close();
            session.close();
            con.close();
            context.close();
        } catch (NamingException | JMSException e) {
            e.printStackTrace();
        }
    }
}
