Java 未使用Jboss主题重新传递消息
我正在使用JBoss4.0.2 GA 我正在使用jbossmq目的地服务中定义的testTopic 在上面的代码中,我使用的是Client_Acknowledge会话模式,但即使我从Listener的onMessage方法抛出NullPointerException,也无法重新传递消息Java 未使用Jboss主题重新传递消息,java,jboss,jms,Java,Jboss,Jms,我正在使用JBoss4.0.2 GA 我正在使用jbossmq目的地服务中定义的testTopic 在上面的代码中,我使用的是Client_Acknowledge会话模式,但即使我从Listener的onMessage方法抛出NullPointerException,也无法重新传递消息 请告诉我是否需要进行任何配置更改才能进行重新传递。对于主题,如果您在阅读消息后(例如,在try/catch块中)调用topicSession.recover,则会立即重新传递消息,如果发生异常 使用JBoss 7
请告诉我是否需要进行任何配置更改才能进行重新传递。对于主题,如果您在阅读消息后(例如,在try/catch块中)调用topicSession.recover,则会立即重新传递消息,如果发生异常 使用JBoss 7、非持久主题、Session.CLIENT_ACKNOWLEDGE和setMessageListener的测试:
在消息被放入死信队列后,立即重新发送10次。主题是否持久?无论如何,我不确定,如果它是在一个主题的情况下,它不是一个队列!。请尝试调用queueSession.recover,而不是静默退出。我很确定,在队列的情况下有一个很大的区别:不调用recover意味着JBoss在超时期间等待一个超时,这些消息将不会被重新传递。使用recover意味着可以立即重新传递消息。根据您的建议,在以下代码行之后:TopicSession pubSession=connection.createTopicSessionfalse,Session.CLIENT_ACKNOWLEDGE;我添加了一行,即pubSession.recover;此外,我现在正在使用非durableTopic,但它不起作用阅读邮件后,请致电recover。因此,不要像System.exit那样什么都不做,而是在退出应用程序之前调用它。我在另一篇文章中读到过,你评论说你无法在jboss中运行它@SAM我并不完美,JBoss也不完美。。。哪个帖子?我说的是这个帖子:
<mbean code="org.jboss.mq.server.jmx.Topic"
name="jboss.mq.destination:service=Topic,name=testTopic">
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
<depends optional-attribute-name="SecurityManager">jboss.mq:service=SecurityManager</depends>
<attribute name="SecurityConf">
<security>
<role name="guest" read="true" write="true"/>
<role name="publisher" read="true" write="true" create="false"/>
<role name="durpublisher" read="true" write="true" create="true"/>
</security>
</attribute>
<attribute name="RedeliveryDelay">0</attribute>
</mbean>
package com.nagarro.client;
import javax.jms.*;
import javax.naming.*;
import java.io.*;
import java.util.Properties;
public class Chat implements javax.jms.MessageListener {
private TopicSession pubSession;
private TopicSession subSession;
private TopicPublisher publisher;
private TopicConnection connection;
private String username;
/* Constructor. Establish JMS publisher and subscriber */
public Chat(String topicName, String username, String password)
throws Exception {
// Obtain a JNDI connection
Properties properties = new Properties();
properties.put("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
properties.put("java.naming.factory.url.pkgs",
"org.jboss.naming:org.jnp.interfaces");
properties.setProperty(Context.PROVIDER_URL, "localhost:1099");
// ... specify the JNDI properties specific to the vendor
InitialContext jndi = new InitialContext(properties);
// Look up a JMS connection factory
TopicConnectionFactory conFactory = (TopicConnectionFactory) jndi
.lookup("TopicConnectionFactory");
// Create a JMS connection
TopicConnection connection = conFactory.createTopicConnection();
// Create two JMS session objects
TopicSession pubSession = connection.createTopicSession(false,
Session.CLIENT_ACKNOWLEDGE);
TopicSession subSession = connection.createTopicSession(false,
Session.CLIENT_ACKNOWLEDGE);
// Look up a JMS topic
Topic chatTopic = (Topic) jndi.lookup(topicName);
// Create a JMS publisher and subscriber
TopicPublisher publisher = pubSession.createPublisher(chatTopic);
TopicSubscriber subscriber = subSession.createSubscriber(chatTopic);
// Set a JMS message listener
subscriber.setMessageListener(this);
// Intialize the Chat application
set(connection, pubSession, subSession, publisher, username);
// Start the JMS connection; allows messages to be delivered
connection.start();
}
/* Initialize the instance variables */
public void set(TopicConnection con, TopicSession pubSess,
TopicSession subSess, TopicPublisher pub, String username) {
this.connection = con;
this.pubSession = pubSess;
this.subSession = subSess;
this.publisher = pub;
this.username = username;
}
/* Receive message from topic subscriber */
public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println(text);
if(textMessage!=null){
throw new NullPointerException();
}
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}
/* Create and send message using topic publisher */
protected void writeMessage(String text) throws JMSException {
TextMessage message = pubSession.createTextMessage();
message.setText(username + " : " + text);
publisher.publish(message);
}
/* Close the JMS connection */
public void close() throws JMSException {
connection.close();
}
/* Run the Chat client */
public static void main(String[] args) {
try {
if (args.length != 3)
System.out.println("Topic or username missing");
// args[0]=topicName; args[1]=username; args[2]=password
Chat chat = new Chat("topic/testTopic", "", "");
// Read from command line
BufferedReader commandLine = new java.io.BufferedReader(
new InputStreamReader(System.in));
// Loop until the word "exit" is typed
while (true) {
String s = commandLine.readLine();
if (s.equalsIgnoreCase("exit")) {
chat.close(); // close down connection
System.exit(0);// exit program
} else
chat.writeMessage(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
topicSubscriber.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
try {
throw new IllegalStateException("Test");
} catch (Exception ex) {
try {
topicSession.recover();
} catch (JMSException e) {
e.printStackTrace();
}
ex.printStackTrace();
}
}
});