如题,我希望用200个线程监听消息,但实际在监听消息的却只有五个。而且并不是我开的线程。
举例来讲,用线程池new 一个用于监听消息的类,并打印当前线程ID。我开的线程明明是29-229,但是onMessage打印当前线程的时候,却一直是19,20,21,22,23线程……why?
下面是代码:红色的部分是打印线程ID的部分。mian类:
public class SenderStart {
public static void main(String[] args) throws IOException, JMSException, NamingException {
//SpringContextLocator是用于加载Spring配置文件的类,可忽略
                  BeanFactory factory=SpringContextLocator.getContext().getBeanFactory();
JmsSender jmsSender = new JmsSender(factory);
jmsSender.start();
}}这个类用于创建队列连接,start方法开启多个线程,监听队列
public class JmsSender {
@SuppressWarnings("unused")
private static final Log log = LogFactory.getLog(JmsSender.class);
private Properties props = null;
private QueueConnectionFactory queueFactory = null;
private Destination sq = null;
private BeanFactory factory = null;
@SuppressWarnings("unchecked")
public JmsSender(BeanFactory beanFactory) throws IOException, JMSException, NamingException { Hashtable ht = new Hashtable();
props = getGlobalConfig(); //从config配置文件中读取配置信息
String providerUrl = props.getProperty("jms.provider.url");
String secupityPrincipal = props.getProperty("jms.security.principal");
String securityCrenentials = props.getProperty("jms.security.crenentials");
String jndiFactoryName = props.getProperty("jms.jndi.factory.name");
String queueConnectionFactory = props.getProperty("jms.queue.connection.factory.name"); ht.put(Context.INITIAL_CONTEXT_FACTORY, jndiFactoryName);
ht.put(Context.PROVIDER_URL, providerUrl);
ht.put(Context.SECURITY_PRINCIPAL, secupityPrincipal);
ht.put(Context.SECURITY_CREDENTIALS, securityCrenentials); Context context = new InitialContext(ht);
sq = (Destination) context.lookup(Constants.SEND_QUEUE);
queueFactory = (QueueConnectionFactory) context.lookup(queueConnectionFactory); factory = beanFactory; }
public void start() throws JMSException {
String poolCount = props.getProperty("jms.thread.pool.count");
QueueConnection connection = queueFactory.createQueueConnection();
if (null != poolCount) {
int p = Integer.parseInt(poolCount);
Executor pool = Executors.newFixedThreadPool(p);
for (int i=0;i<p;i++) {
pool.execute(new JmsSenderThread(connection, sq, factory));
}
}
connection.start();
}
protected Properties getGlobalConfig() throws IOException {
Properties config = new Properties();
config.load(this.getClass().getResourceAsStream("/config.properties"));
return config;
}}//线程池初始化的线程,用传入的connection去创建多个session
package cn.rntd.sender.run;
public class JmsSenderThread implements Runnable, Serializable {
private static final Log log = LogFactory.getLog(JmsSenderThread.class);
private QueueConnection connection;
private Destination ds;
private BeanFactory factory = null;
public JmsSenderThread(QueueConnection qConnection, Destination destination, BeanFactory bFactory) throws JMSException {
factory = bFactory;
connection = qConnection;
ds=destination;
}
public void run() {
try {
log.info(Thread.currentThread().getId()); QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer receiver = session.createConsumer(ds);
receiver.setMessageListener(new MsgListener(factory));
} catch (JMSException e) {
log.error("", e);
// throw new RuntimeException(e);
}
}}消息监听类
package cn.rntd.sender.run;
public class MsgListener implements MessageListener {
private static final Log log = LogFactory.getLog(MsgListener.class);
private BeanFactory factory = null;
public MsgListener(BeanFactory beanFactory) {
factory = beanFactory;
}
public void onMessage(Message message) {
// 业务处理调用接口
log.info("[当前监听消息线程ID]"+Thread.currentThread().getId()); ObjectMessage objMsg = (ObjectMessage) message;
try {
                      ……业务处理部分,可忽略

} catch (JMSException e) {
log.error("", e);
throw new RuntimeException(e);
}
}}

解决方案 »

  1.   

    貌似问题在这里:虽然我new 了200个JmsSenderThread,
    每个JmsSenderThread又有receiver.setMessageListener(new MsgListener(factory)); 
    照理说应该也new 了new MsgListener(factory)200个,但是不知为什么,只有5个……
      

  2.   

    貌似是我理解错误。= =|||
    实际是:
    new 了200个JmsSenderThread,打印的当前线程ID为30-230
    new MsgListener(factory),打印的当前线程ID为30-230,与上面对应。
    但实际接收消息时:onMessage方法打印的当前线程ID为19-23……这个落差……
    唉……
      

  3.   

    看看你JMS的配置,是不是对监听数有限制?
      

  4.   

    jms的监听数我连配都没配……到哪里配啊