Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8934373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:46:10+00:00 2026-06-15T09:46:10+00:00

I just tarted to work with JMS So i took a example ,and it

  • 0

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) {

    }
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:46:11+00:00Added an answer on June 15, 2026 at 9:46 am

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a knowledge question which I would like to ask: For example, I have
Just checking my JS and I have an error, but I cannot see where.
Just learning the world of jquery, and all my googling gives examples like this:
Just a simple question, but can't get an answer on my own. In memory
Just curious (maybe this is an implementation detail so you may not know) but
Just like socket Begin\End methods use IOCP, would that also be true for SSLStream
Just like title, I want to ask, how to adding pdf files with upload
Just like the topic says. I have a DLL that provides a DAL, and
Just like continue is used to break current iteration and proceed with the next,
Just recently I've installed the Qt libraries on my computer, and as a complete

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.