[英]How do I start a threaded session in Active MQ 5.x
I guess it is kind of a standard knowledge question, but will current_session
run threaded or not?我想这是一个标准的知识问题,但是
current_session
会以线程方式运行吗?
ActiveMQSession current_session = (ActiveMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Thread sessionThreads = new Thread(current_session);
sessionThreads.start();
If not, can please someone show me a code example how I run a session threaded?如果没有,请有人给我看一个代码示例,我如何运行 session 线程?
What I want is concurrent sessions with producers/consumers which write/listen to their specific queues.我想要的是与写/听他们特定队列的生产者/消费者的并发会话。 I already tried to write a custom Thread by passing the connection to the thread, but when I created Producers I run into an error, 'that I can't start a Producer on an unregistered session'.
我已经尝试通过将连接传递给线程来编写自定义线程,但是当我创建生产者时遇到错误,“我无法在未注册的会话上启动生产者”。
There is no constructor for java.lang.Thread
that will accept a org.apache.activemq.ActiveMQSession
object. java.lang.Thread
没有可以接受org.apache.activemq.ActiveMQSession
object 的构造函数。 Your code won't even compile let alone run.您的代码甚至不会编译,更不用说运行了。
Here's a simple client that will create threads for producing and consuming messages:这是一个简单的客户端,它将创建用于生成和使用消息的线程:
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
public class MyMultiThreadedApp {
class MyConsumer extends Thread {
private final Connection connection;
private final Destination destination;
MyConsumer(Connection connection, Destination destination) {
this.connection = connection;
this.destination = destination;
}
@Override
public void run() {
try (Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE)) {
MessageConsumer messageConsumer = session.createConsumer(destination);
connection.start();
Message message = messageConsumer.receive(5000);
if (message == null) {
System.out.println("Did not receive message within the allotted time.");
return;
}
System.out.println("Received message: " + message);
} catch (Throwable e) {
e.printStackTrace();
return;
}
}
}
class MyProducer extends Thread {
private final Connection connection;
private final Destination destination;
MyProducer(Connection connection, Destination destination) {
this.connection = connection;
this.destination = destination;
}
@Override
public void run() {
try (Session session = connection.createSession(Session.AUTO_ACKNOWLEDGE)) {
MessageProducer messageProducer = session.createProducer(destination);
messageProducer.send(session.createTextMessage("My message"));
System.out.println("Sent message");
} catch (Throwable e) {
e.printStackTrace();
return;
}
}
}
public static void main(String... args) throws Exception {
MyMultiThreadedApp myMultiThreadedApp = new MyMultiThreadedApp();
InitialContext initialContext = null;
initialContext = new InitialContext();
Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
Connection connection = cf.createConnection();
Thread myConsumer = myMultiThreadedApp.runConsumer(connection, queue);
Thread myProducer = myMultiThreadedApp.runProducer(connection, queue);
myConsumer.join();
myProducer.join();
}
private Thread runConsumer(Connection connection, Destination destination) {
MyConsumer myConsumer = new MyConsumer(connection, destination);
myConsumer.start();
return myConsumer;
}
private Thread runProducer(Connection connection, Destination destination) {
MyProducer myProducer = new MyProducer(connection, destination);
myProducer.start();
return myProducer;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.