I am new to JMS and APIs. I started working on a messaging platform to try and connect the Glassfish local server to start sending and receiving messages through the server. However, I don't understand how to set up the Initial Context to recognize where the server is, and what to connect to. When I run this, I get a NoInitialContextException.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.naming.*;
import javax.jms.*;
public class Sender {
public static void main(String[] args) {
try {
InitialContext ctx = new InitialContext();
QueueConnectionFactory f = (QueueConnectionFactory)ctx.lookup("java:comp/DefaultJMSConnectionFactory");
QueueConnection con = f.createQueueConnection();
con.start();
QueueSession ses=con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
//3) get the Queue object
Queue t=(Queue)ctx.lookup("PTPQueue");
//4)create QueueSender object
QueueSender sender=ses.createSender(t);
//5) create TextMessage object
TextMessage msg=ses.createTextMessage();
//6) write message
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Enter Msg, end to terminate:");
String s=b.readLine();
if (s.equals("end")) {
break;
}
msg.setText(s);
//7) send message
sender.send(msg);
System.out.println("Message successfully sent.");
}
//8) connection close
con.close();
} catch(Exception e){
System.out.println(e);
}
}
}
I expected it to try to connect with the server, but nothing happened. How do I change this so that I can access the server? I have heard of a jndi.properties file but I am not sure how to use that here. I am using Intellij, so is there an easy fix to connect the server there and work from there?