0
我有一個特定主題的發件人和收件人。我將發送者和接收者作爲WAS 7.0上的servlet運行。 正在WAS上設置Topic
和Topic Connection Factory
。但我無法收到發送的消息。當我嘗試使用Queue而不是Topic時,它工作正常。JMS - 在WebSphere Application Server上使用主題7
下面是我使用的代碼。
public class CommonServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
private static boolean initialized = false;
private static InitialContext initialContext = null;
private ConnectionFactory connectionFactory = null;
private Destination destination = null;
protected final void initialize(){
try{
if(initialized == false){
// Get JNDI initial context
initialContext = new InitialContext();
// Flag as initialized
initialized = true;
}
}
catch(Throwable t){
System.out.println(t.getMessage());
}
}
/**
* @return
*/
protected final Destination getDestination(){
if(destination != null){
return destination;
}
try{
destination = (Destination) initialContext.lookup("jms/TestTopic");
}
catch(NamingException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
return destination;
}
/**
* @return
*/
protected final ConnectionFactory getConnectionFactory(){
try{
connectionFactory = (ConnectionFactory) initialContext.lookup("jms/TestTopicCF");
}
catch(NamingException e){
e.printStackTrace();
}
return connectionFactory;
}
}
發件人servlet類
public class Sender extends CommonServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
System.out.println("inside do get of sender");
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
String message = req.getParameter("message");
sendMessage(message);
}
private void sendMessage(String messageText){
initialize();
try{
ConnectionFactory factory = getConnectionFactory();
Destination destination = getDestination();
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer sender = session.createProducer(destination);
TextMessage txtMsg = session.createTextMessage(messageText);
sender.send(txtMsg);
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
接收機servlet類
public class Receiver extends CommonServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
getMessage();
}
private void getMessage(){
initialize();
ConnectionFactory factory = getConnectionFactory();
Destination destination = getDestination();
try{
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer receiver = session.createConsumer(destination);
Message message = receiver.receive(4000);
System.out.println(message);//COMING AS NULL
}
catch(JMSException e){
e.printStackTrace();
}
}
}
兩個TestTopic
和TestTopicCF
在WAS配置管理控制檯下資源> JMS>主題連接工廠和主題部分。運行應用程序時沒有例外。 如果我使用創建的隊列和隊列連接工廠,它工作正常。 有什麼我需要專門去做主題工作?