Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ActiveMQ生产者多队列一个会话_Java_Jms_Activemq - Fatal编程技术网

Java ActiveMQ生产者多队列一个会话

Java ActiveMQ生产者多队列一个会话,java,jms,activemq,Java,Jms,Activemq,如何在ActiveMQ中使用持久连接/会话将消息排队到不同的队列 我所做的: public class ActiveMQProducer { private static final Logger LOGGER = Logger.getLogger(ActiveMQProducer.class); private Connection connection; private MessageProducer producer; private Session se

如何在ActiveMQ中使用持久连接/会话将消息排队到不同的队列

我所做的:

public class ActiveMQProducer {

    private static final Logger LOGGER = Logger.getLogger(ActiveMQProducer.class);
    private Connection connection;
    private MessageProducer producer;
    private Session session;
    String activeMQConnection;

    public ActiveMQProducer() throws ConfigurationException, JMSException {
        activeMQConnection = ActiveMQPropertyManagerFactory.getInstance().getString("active.mq.url");
    }

    public void setupActiveMQ(String queueName) throws JMSException {

        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(activeMQConnection);
        factory.setRejectedTaskHandler(new ThreadPoolExecutor.CallerRunsPolicy());

        connection = factory.createConnection();
        connection.start();

        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(queueName);
        producer = session.createProducer(queue);

    }

    public void getConnection(String queueName) throws JMSException {

        if (connection == null || session == null) {
            Object object = new Object();
            synchronized (object) {
                setupActiveMQ(queueName);
            }
        }
    }

    public <T extends Serializable> T sendToActiveMQ(String queueName, T t) throws JMSException {
        getConnection(queueName);
        ObjectMessage message = session.createObjectMessage(t);
        producer.send(message);
        return null;
    }

    public void sendMessageToActiveMQ(String queueName, String message) throws JMSException {
        getConnection(queueName);
        TextMessage toSend = session.createTextMessage(message);
        producer.send(toSend);
    }
}
正确的处理方法是什么?我需要向大约5个队列发送不同的消息,我是否应该打开一个新连接,排队并关闭连接,是否仍然需要保持会话/连接的持久性

谢谢。

这里有一些解决方案:

每个目的地1个生产商:

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ConfigurationException;
import org.apache.log4j.Logger;

public class ActiveMQProducer {

    private static final Logger LOGGER = Logger.getLogger(ActiveMQProducer.class);
    private Connection connection;
    private Session session;
    String activeMQConnection;
    Map<String, MessageProducer> producers = Collections.synchronizedMap(new HashMap<String, MessageProducer>());
    Thread shutdownHook = new Thread(new Runnable() {
        @Override
        public void run() {
            close();
        }
    });

    public ActiveMQProducer() throws ConfigurationException, JMSException {
        activeMQConnection = ActiveMQPropertyManagerFactory.getInstance().getString("active.mq.url");
        setupActiveMQ();
        Runtime.getRuntime().addShutdownHook(shutdownHook);
    }

    public void setupActiveMQ() throws JMSException {
        close();
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(activeMQConnection);
        factory.setRejectedTaskHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        connection = factory.createConnection();
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    }

    @Override
    protected void finalize() throws Throwable {
        close();
        super.finalize();
    }

    public void close() {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            }
            connection = null;
        }
    }

    public void getConnection() throws JMSException {
        if (connection == null || session == null) {
            setupActiveMQ();
        }
    }

    public MessageProducer getProducer(String queueName) throws JMSException {
        getConnection();
        MessageProducer producer = producers.get(queueName);
        if (producer == null) {
            Queue queue = session.createQueue(queueName);
            producer = session.createProducer(queue);
            producers.put(queueName, producer);
        }
        return producer;
    }

    public <T extends Serializable> T sendToActiveMQ(String queueName, T t) throws JMSException {
        MessageProducer producer = getProducer(queueName);
        ObjectMessage message = session.createObjectMessage(t);
        producer.send(message);
        return null;
    }

    public void sendMessageToActiveMQ(String queueName, String message) throws JMSException {
        MessageProducer producer = getProducer(queueName);
        TextMessage toSend = session.createTextMessage(message);
        producer.send(toSend);
    }
}
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ConfigurationException;
import org.apache.log4j.Logger;

public class ActiveMQProducer2 {

    private static final Logger LOGGER = Logger.getLogger(ActiveMQProducer2.class);
    private Connection connection;
    private Session session;
    String activeMQConnection;
    Map<String, Destination> destinations = Collections.synchronizedMap(new HashMap<String, Destination>());
    private MessageProducer producer;
    Thread shutdownHook = new Thread(new Runnable() {
        @Override
        public void run() {
            close();
        }
    });

    public ActiveMQProducer2() throws ConfigurationException, JMSException {
        activeMQConnection = ActiveMQPropertyManagerFactory.getInstance().getString("active.mq.url");
        setupActiveMQ();
        Runtime.getRuntime().addShutdownHook(shutdownHook);
    }

    public void setupActiveMQ() throws JMSException {
        close();
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(activeMQConnection);
        factory.setRejectedTaskHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        connection = factory.createConnection();
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(session.createTemporaryQueue());
    }

    @Override
    protected void finalize() throws Throwable {
        close();
        super.finalize();
    }

    public void close() {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            }
            connection = null;
        }
    }

    public void getConnection() throws JMSException {
        if (connection == null || session == null) {
            setupActiveMQ();
        }
    }

    public Destination getDestination(String queueName) throws JMSException {
        getConnection();
        Destination destination = destinations.get(queueName);
        if (destination == null) {
            destination = session.createQueue(queueName);
            destinations.put(queueName, destination);
        }
        return destination;
    }

    public <T extends Serializable> T sendToActiveMQ(String queueName, T t) throws JMSException {
        Destination destination = getDestination(queueName);
        ObjectMessage message = session.createObjectMessage(t);
        producer.send(destination, message);
        return null;
    }

    public void sendMessageToActiveMQ(String queueName, String message) throws JMSException {
        Destination destination = getDestination(queueName);
        TextMessage toSend = session.createTextMessage(message);
        producer.send(destination, toSend);
    }
}
import java.io.Serializable;
导入java.util.Collections;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.concurrent.ThreadPoolExecutor;
导入javax.jms.Connection;
导入javax.jms.jmsception;
导入javax.jms.MessageProducer;
导入javax.jms.ObjectMessage;
导入javax.jms.Queue;
导入javax.jms.Session;
导入javax.jms.TextMessage;
导入org.apache.activemq.ActiveMQConnectionFactory;
导入org.apache.activemq.ConfigurationException;
导入org.apache.log4j.Logger;
公共类ActiveMQProducer{
私有静态最终记录器Logger=Logger.getLogger(ActiveMQProducer.class);
专用连接;
非公开会议;
字符串连接;
Map producers=Collections.synchronizedMap(新HashMap());
Thread shutdownHook=新线程(new Runnable(){
@凌驾
公开募捐{
close();
}
});
public ActiveMQProducer()引发ConfigurationException,JMSExException{
activeMQConnection=ActiveMQPropertyManagerFactory.getInstance().getString(“active.mq.url”);
setupActiveMQ();
Runtime.getRuntime().addShutdownHook(shutdownHook);
}
public void setupActiveMQ()引发JMSExException{
close();
ActiveMQConnectionFactory工厂=新的ActiveMQConnectionFactory(activeMQConnection);
setRejectedTaskHandler(新的ThreadPoolExecutor.CallerRunPolicy());
connection=factory.createConnection();
connection.start();
会话=connection.createSession(false,session.AUTO_-ACKNOWLEDGE);
}
@凌驾
受保护的void finalize()抛出可丢弃的{
close();
super.finalize();
}
公众假期结束(){
if(连接!=null){
试一试{
connection.close();
}捕获(例外e){
}
连接=空;
}
}
public void getConnection()引发JMSException{
if(连接==null | |会话==null){
setupActiveMQ();
}
}
public MessageProducer getProducer(字符串queueName)引发JMSException{
getConnection();
MessageProducer=producers.get(queueName);
if(producer==null){
Queue Queue=session.createQueue(queueName);
producer=session.createProducer(队列);
producers.put(队列名称,producer);
}
回报生产者;
}
public T sendToActiveMQ(字符串queueName,tt)引发JMSException{
MessageProducer=getProducer(queueName);
ObjectMessage=session.createObjectMessage(t);
生产者。发送(消息);
返回null;
}
public void sendMessageToActiveMQ(字符串队列名称,字符串消息)引发JMSExException{
MessageProducer=getProducer(queueName);
TextMessage toSend=session.createTextMessage(消息);
制作人发送(toSend);
}
}
所有目的地的1个生产商:

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ConfigurationException;
import org.apache.log4j.Logger;

public class ActiveMQProducer {

    private static final Logger LOGGER = Logger.getLogger(ActiveMQProducer.class);
    private Connection connection;
    private Session session;
    String activeMQConnection;
    Map<String, MessageProducer> producers = Collections.synchronizedMap(new HashMap<String, MessageProducer>());
    Thread shutdownHook = new Thread(new Runnable() {
        @Override
        public void run() {
            close();
        }
    });

    public ActiveMQProducer() throws ConfigurationException, JMSException {
        activeMQConnection = ActiveMQPropertyManagerFactory.getInstance().getString("active.mq.url");
        setupActiveMQ();
        Runtime.getRuntime().addShutdownHook(shutdownHook);
    }

    public void setupActiveMQ() throws JMSException {
        close();
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(activeMQConnection);
        factory.setRejectedTaskHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        connection = factory.createConnection();
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    }

    @Override
    protected void finalize() throws Throwable {
        close();
        super.finalize();
    }

    public void close() {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            }
            connection = null;
        }
    }

    public void getConnection() throws JMSException {
        if (connection == null || session == null) {
            setupActiveMQ();
        }
    }

    public MessageProducer getProducer(String queueName) throws JMSException {
        getConnection();
        MessageProducer producer = producers.get(queueName);
        if (producer == null) {
            Queue queue = session.createQueue(queueName);
            producer = session.createProducer(queue);
            producers.put(queueName, producer);
        }
        return producer;
    }

    public <T extends Serializable> T sendToActiveMQ(String queueName, T t) throws JMSException {
        MessageProducer producer = getProducer(queueName);
        ObjectMessage message = session.createObjectMessage(t);
        producer.send(message);
        return null;
    }

    public void sendMessageToActiveMQ(String queueName, String message) throws JMSException {
        MessageProducer producer = getProducer(queueName);
        TextMessage toSend = session.createTextMessage(message);
        producer.send(toSend);
    }
}
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ConfigurationException;
import org.apache.log4j.Logger;

public class ActiveMQProducer2 {

    private static final Logger LOGGER = Logger.getLogger(ActiveMQProducer2.class);
    private Connection connection;
    private Session session;
    String activeMQConnection;
    Map<String, Destination> destinations = Collections.synchronizedMap(new HashMap<String, Destination>());
    private MessageProducer producer;
    Thread shutdownHook = new Thread(new Runnable() {
        @Override
        public void run() {
            close();
        }
    });

    public ActiveMQProducer2() throws ConfigurationException, JMSException {
        activeMQConnection = ActiveMQPropertyManagerFactory.getInstance().getString("active.mq.url");
        setupActiveMQ();
        Runtime.getRuntime().addShutdownHook(shutdownHook);
    }

    public void setupActiveMQ() throws JMSException {
        close();
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(activeMQConnection);
        factory.setRejectedTaskHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        connection = factory.createConnection();
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(session.createTemporaryQueue());
    }

    @Override
    protected void finalize() throws Throwable {
        close();
        super.finalize();
    }

    public void close() {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            }
            connection = null;
        }
    }

    public void getConnection() throws JMSException {
        if (connection == null || session == null) {
            setupActiveMQ();
        }
    }

    public Destination getDestination(String queueName) throws JMSException {
        getConnection();
        Destination destination = destinations.get(queueName);
        if (destination == null) {
            destination = session.createQueue(queueName);
            destinations.put(queueName, destination);
        }
        return destination;
    }

    public <T extends Serializable> T sendToActiveMQ(String queueName, T t) throws JMSException {
        Destination destination = getDestination(queueName);
        ObjectMessage message = session.createObjectMessage(t);
        producer.send(destination, message);
        return null;
    }

    public void sendMessageToActiveMQ(String queueName, String message) throws JMSException {
        Destination destination = getDestination(queueName);
        TextMessage toSend = session.createTextMessage(message);
        producer.send(destination, toSend);
    }
}
import java.io.Serializable;
导入java.util.Collections;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.concurrent.ThreadPoolExecutor;
导入javax.jms.Connection;
导入javax.jms.Destination;
导入javax.jms.jmsception;
导入javax.jms.MessageProducer;
导入javax.jms.ObjectMessage;
导入javax.jms.Session;
导入javax.jms.TextMessage;
导入org.apache.activemq.ActiveMQConnectionFactory;
导入org.apache.activemq.ConfigurationException;
导入org.apache.log4j.Logger;
公共类ActiveMQProducer2{
私有静态最终记录器Logger=Logger.getLogger(ActiveMQProducer2.class);
专用连接;
非公开会议;
字符串连接;
映射目的地=Collections.synchronizedMap(新HashMap());
私人信息生产者;
Thread shutdownHook=新线程(new Runnable(){
@凌驾
公开募捐{
close();
}
});
public ActiveMQProducer2()引发ConfigurationException,JMSExException{
activeMQConnection=ActiveMQPropertyManagerFactory.getInstance().getString(“active.mq.url”);
setupActiveMQ();
Runtime.getRuntime().addShutdownHook(shutdownHook);
}
public void setupActiveMQ()引发JMSExException{
close();
ActiveMQConnectionFactory工厂=新的ActiveMQConnectionFactory(activeMQConnection);
setRejectedTaskHandler(新的ThreadPoolExecutor.CallerRunPolicy());
connection=factory.createConnection();
connection.start();
会话=connection.createSession(false,session.AUTO_-ACKNOWLEDGE);
producer=session.createProducer(session.createTemporaryQueue());
}
@凌驾
受保护的void finalize()抛出可丢弃的{
close();
super.finalize();
}
公众假期结束(){
if(连接!=null){
试一试{
connection.close();
}捕获(例外e){
}
连接=空;
}
}
public void getConnection()引发JMSException{
if(连接==null | |会话==null){
setupActiveMQ();
}
}
公共目标getDestination(字符串queueName)引发JMSExException{
getConnection();
Destination=destinations.get(queueName);
如果(目标==null){
目的地=东南