Reputation: 751
I just tarted to work with JMS So i took a example ,and it worked like a charm ! When i typed a message in the console the subscribers received it,but the example is based on only a file,so i decidet to split it in a publisher file and a subscriver file but it wont work ,the subscriber doesn't read the message ! I inserted some code from this other example but still nothing ,and i cant understand why as my program compile perfectly without errors ,Do someone have a idea why it doesn't read the message?
My Subscriver.java file is:
package main;
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.sun.messaging.Destination;
public class Subscriver1 {
private TopicSession subSession;
private TopicSubscriber subscriver;
private TopicConnection connection;
private String username;
Destination dest;
public Subscriver1(String topicName, String username, String password)
throws NamingException, JMSException {
Properties env = new Properties();
InitialContext jndi = new InitialContext(env);
TopicConnectionFactory conFactory = (TopicConnectionFactory) jndi
.lookup("topicConn");
TopicConnection connection = conFactory.createTopicConnection(username,
password);
TopicSession subSession = connection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
Topic chatTopic = (Topic) jndi.lookup(topicName);
TopicSubscriber subscriver = subSession.createSubscriber(chatTopic,
null, true);
Destination dest = (Destination) jndi.lookup(topicName);
set(connection, subSession, subscriver, username, dest);
connection.start();
}
private void set(TopicConnection connection2, TopicSession subSession2,
TopicSubscriber subscriver2, String username2, Destination dest2) {
this.connection = connection2;
this.subSession = subSession2;
this.subscriver = subscriver2;
this.username = username2;
this.dest = dest2;
}
public void close() throws JMSException {
connection.close();
}
public void read() throws JMSException {
while (true) {
MessageConsumer consumer = subSession.createConsumer(dest);
;
Message m = consumer.receive(1);
if (m != null) {
if (m instanceof TextMessage) {
TextMessage message = (TextMessage) m;
System.out.println("Reading message: " + message.getText());
} else {
break;
}
}
}
}
public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println(text);
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}
public static void main(String[] args) throws NamingException, JMSException {
Subscriver1 lexoMesazhin = new Subscriver1("kanaliTopic", "user",
"user");
lexoMesazhin.read();
lexoMesazhin.close();
}
}
and my file Publisher.java file is
package main;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Publisher {
private TopicSession pubSession;
private TopicPublisher publisher;
private TopicConnection connection;
private String username;
public Publisher(String topicName, String username, String password)
throws NamingException, JMSException {
Properties env = new Properties();
InitialContext jndi = new InitialContext(env);
TopicConnectionFactory conFactory = (TopicConnectionFactory) jndi
.lookup("topicConn");
TopicConnection connection = conFactory.createTopicConnection(username,
password);
TopicSession pubSession = connection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
Topic chatTopic = (Topic) jndi.lookup(topicName);
TopicPublisher publisher = pubSession.createPublisher(chatTopic);
set(connection, pubSession, publisher, username);
connection.start();
}
private void set(TopicConnection connection2, TopicSession pubSession2,
TopicPublisher publisher2, String username2) {
this.connection = connection2;
this.pubSession = pubSession2;
this.publisher = publisher2;
this.username = username2;
}
protected void createMesage(String text) throws JMSException {
TextMessage mesazhi = pubSession.createTextMessage(text);
mesazhi.setText(username + " >> " + text);
}
public void close() throws JMSException {
connection.close();
}
public static void main(String[] args) throws NamingException,
JMSException, IOException {
Publisher dergonMesazhin = new Publisher("kanaliTopic", "andi", "andi");
BufferedReader commandLine = new java.io.BufferedReader(
new InputStreamReader(System.in));
try {
while (true) {
String s = commandLine.readLine();
if (s.equalsIgnoreCase("exit")) {
dergonMesazhin.close();
System.exit(0);
} else
dergonMesazhin.createMesage(s);
}
} catch (IOException e) {
}
}
}
Upvotes: 0
Views: 709
Reputation: 15241
You forgot to publish the message, i.e. you should add a call publisher.publish(message)
in createMesage
method. Currently, you are just creating a TextMessage object without sending it anywhere, so technically it's not problem in your subscriber, but in your producer :)
Upvotes: 1
Reputation: 2625
Where is your JNDI server located? You pass an empty Properties object into the InitialContext.
Properties env = new Properties();
InitialContext jndi = new InitialContext(env);
You should places some sensible values into the properties object, such as server url, etc., depending on the server hosting your JNDI service.
Also try omitting passing env and simply call new InitialContext (); That would read the correct properties from a file on your classpath (should be there in glassfish).
Upvotes: 0