JMS Producer for testing purposes written in Java - error in JNDI?

59 Views Asked by At

I've never used Java before, and I just need a basic JMS producer for a WebLogic server. I'm using JDK 1.8 and the wlfullclient.jar as library that developers sent to me as a reference. I usually write test automation in Python, but in this case I need Java just to create this producer. I've found some snippets online and after a day of copy-paste-struggle I've come to a buildable code.

I have a problem (I think but it's the first I've encountered after a successful build).

Cannot instantiate class: jms/test/v1/ConnectionFactory

Inside the code I give to the Java program connection arguments and JNDI's

  • Server name
  • Host: 192.xxx.xxx.xxx
  • Port: 8250
  • Destination: InboundMessage
  • JNDI Factory: jms/test/v1/ConnectionFactory
  • JNDI Name: jms/test/v1/InboundMessage
  • Username : *****
  • Password: *****

Those are the connection details I have.

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;

public class JmsProducer {

    private static final String USERNAME = "tuo-nome-utente";
    private static final String PASSWORD = "tua-password";

    public static void main(String[] args) {
        if (args.length < 7) {
            System.out.println("Parametri mancanti. Fornisci tutti i parametri richiesti.");
            System.out.println("Utilizzo: java JmsProducer <serverName> <host> <port> <queue/topic> <jndiFactory> <jndiName> <tarFilePath>");
            return;
        }

        String serverName = args[0];
        String host = args[1];
        int port = Integer.parseInt(args[2]);
        String destinationName = args[3];
        String jndiFactory = args[4];
        String jndiName = args[5];
        String tarFilePath = args[6];

        // Crea il contesto JNDI
        Hashtable<String, String> env = new Hashtable<>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, jndiFactory);
        env.put(Context.PROVIDER_URL, "t3://" + host + ":" + port);
        env.put(Context.SECURITY_PRINCIPAL, USERNAME);
        env.put(Context.SECURITY_CREDENTIALS, PASSWORD);

        try {
            InitialContext context = new InitialContext(env);
            System.out.println("Inizializzazione del programma...");

            // Controlla la connessione al server
            System.out.println("Verifica la connessione al server " + serverName + "...");
            try {
                context.lookup("jms/" + jndiName); // Proviamo a fare una ricerca per verificare la connessione
                System.out.println("Connessione al server " + serverName + " stabilita con successo.");
            } catch (NamingException e) {
                System.out.println("Impossibile stabilire la connessione al server " + serverName + ".");
                return;
            }

            // Cerca la connection factory
            ConnectionFactory connectionFactory;
            try {
                connectionFactory = (ConnectionFactory) context.lookup("jms/" + jndiName);
            } catch (NamingException e) {
                System.out.println("Impossibile trovare la connection factory. Verifica il nome JNDI specificato.");
                return;
            }

            // Crea la connessione e la sessione
            Connection connection;
            try {
                connection = connectionFactory.createConnection();
            } catch (JMSException e) {
                System.out.println("Impossibile creare la connessione. Verifica le credenziali o la configurazione del server.");
                return;
            }
            Session session;
            try {
                session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            } catch (JMSException e) {
                System.out.println("Impossibile creare la sessione.");
                return;
            }

            // Cerca la destinazione (coda o topic)
            Destination destination;
            try {
                destination = (Destination) context.lookup(destinationName);
            } catch (NamingException e) {
                System.out.println("Impossibile trovare la destinazione. Verifica il nome JNDI specificato.");
                return;
            }

            // Crea il producer e il messaggio
            MessageProducer producer;
            try {
                producer = session.createProducer(destination);
            } catch (JMSException e) {
                System.out.println("Impossibile creare il producer.");
                return;
            }
            BytesMessage message;
            try {
                message = session.createBytesMessage();
            } catch (JMSException e) {
                System.out.println("Impossibile creare il messaggio.");
                return;
            }

            // Legge il file .tar e lo imposta come payload del messaggio
            File tarFile = new File(tarFilePath);
            byte[] buffer = new byte[(int) tarFile.length()];
            FileInputStream fileInputStream = new FileInputStream(tarFile);
            int bytesRead = fileInputStream.read(buffer);
            if (bytesRead == -1) {
                System.out.println("Impossibile leggere il file: " + tarFilePath);
                return;
            }
            try {
                message.writeBytes(buffer);
            } catch (JMSException e) {
                System.out.println("Impossibile impostare il payload del messaggio.");
                return;
            }

            // Invia il messaggio
            try {
                producer.send(message);
                System.out.println("Messaggio inviato con successo alla destinazione " + destinationName);
            } catch (JMSException e) {
                System.out.println("Impossibile inviare il messaggio.");
                return;
            }

            // Rilascia le risorse
            try {
                producer.close();
                session.close();
                connection.close();
                fileInputStream.close();
            } catch (JMSException | IOException e) {
                System.out.println("Impossibile rilasciare correttamente le risorse.");
            }
        } catch (NamingException e) {
            System.out.println("Si è verificato un'errore NamingException: " + e.getMessage());
        } catch (FileNotFoundException e) {
            System.out.println("File non trovato: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("Si è verificato un errore di I/O: " + e.getMessage());
        } catch (Exception e) {
            System.out.println("Si è verificato un errore: " + e.getMessage());
        }
    }
}

There are a lot of errors probably, but I've never written something in Java. I just need that damned .tar file uploaded.

It seems like it doesn't like the path string as a JNDI factory field as it looks for a class. I'm lost.

0

There are 0 best solutions below