有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在ActiveMQ中获取远程代理上的目标列表

我想知道在不使用JMX的情况下,如何在Java的ActiveMQ中的远程代理上获取目的地(队列或主题)列表。我试图通过DestinationSource类获取主题,但没有成功。在this线程中,我发现必须在客户机和代理上启用咨询支持。我发现默认情况下会启用咨询支持

这是我的经纪人

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import org.apache.activemq.api.core.TransportConfiguration;
import org.apache.activemq.core.config.Configuration;
import org.apache.activemq.core.config.impl.ConfigurationImpl;
import org.apache.activemq.core.remoting.impl.netty.NettyAcceptorFactory;
import org.apache.activemq.core.server.ActiveMQServer;
import org.apache.activemq.core.server.ActiveMQServers;

public class Server {
    public static void main(final String arg[]) throws Exception
    {
        try
        {
            // Step 1. Create the Configuration, and set the properties accordingly
            Configuration configuration = new ConfigurationImpl();
            //we only need this for the server lock file
            configuration.setJournalDirectory("target/data/journal");
            configuration.setPersistenceEnabled(false); // http://activemq.apache.org/what-is-the-difference-between-persistent-and-non-persistent-delivery.html
            configuration.setSecurityEnabled(false); // http://activemq.apache.org/security.html
            /**
             * this map with configuration values is not necessary (it configures the default values).
             * If you want to modify it to run the example in two different hosts, remember to also
             * modify the client's Connector at {@link EmbeddedRemoteExample}.
             */
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("host", "localhost");
            map.put("port", 61616);

            // https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/5/html/HornetQ_User_Guide/ch14s04.html
            TransportConfiguration transpConf = new TransportConfiguration(NettyAcceptorFactory.class.getName(),map);

            HashSet<TransportConfiguration> setTransp = new HashSet<TransportConfiguration>();
            setTransp.add(transpConf);

            configuration.setAcceptorConfigurations(setTransp); // https://github.com/apache/activemq-6/blob/master/activemq-server/src/main/java/org/apache/activemq/spi/core/remoting/Acceptor.java

            // Step 2. Create and start the server
            ActiveMQServer server = ActiveMQServers.newActiveMQServer(configuration);
            server.start();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            throw e;
        }
    }
}

还有客户,我想在那里创建一个主题

import java.util.Set;

import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.advisory.DestinationSource;
import org.apache.activemq.command.ActiveMQTopic;

public class MessageConsumer implements ExceptionListener{

    ActiveMQConnection connection = null;
    javax.jms.MessageConsumer consumer = null;
    Session session = null;

    public MessageConsumer(){
        try {

            // create a new intial context, which loads from jndi.properties file
            javax.naming.Context context = new javax.naming.InitialContext();

            // Create a ConnectionFactory
            ActiveMQConnectionFactory connectionFactory = (org.apache.activemq.ActiveMQConnectionFactory)context.lookup("ConnectionFactory");

            // Create a Connection
            connection= (ActiveMQConnection) connectionFactory.createConnection();

            DestinationSource destSource = connection.getDestinationSource();

            // Create a Session
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // Create the destination (Topic or Queue)
            Destination destination = new ActiveMQTopic("MyTopic");

            // Create a MessageConsumer from the Session to the Topic or Queue
            consumer = session.createConsumer(destination);

            connection.start();

            Thread.sleep(3000);

            Set<ActiveMQTopic> b = destSource.getTopics();

            MessageListener listener = new MessageListener() {
                public void onMessage(Message message) {
                    try {
                        if (message instanceof TextMessage) {
                            TextMessage textMessage = (TextMessage) message;
                            System.out.println("Received message"
                                    + textMessage.getText() + "'");
                        }
                    } catch (JMSException e) {
                        System.out.println("Caught:" + e);
                        e.printStackTrace();
                    }
                }
            };

            consumer.setMessageListener(listener);

        } catch (Exception e) {
            System.out.println("Caught: " + e);
            e.printStackTrace();
        }
    }

    @Override
    public void onException(JMSException exception) {
        System.out.println("JMS Exception occured.  Shutting down client.");        
    }

    public void close(){

        // Clean up
        try {
            consumer.close();
            session.close();
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

}

来源。getTopics()返回一组0个主题


共 (0) 个答案