热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

weblogic7JMS开发,两种连接工厂都试了,topicproducer已经将消息发布至主题,但同步接收者接受不到,高手请教

我用的weblogic7开发,有两种情况:1。用的是weblogic标准的JMSConnectionFactory,编写的topicproducer已经发布成功(console已经按照输出
我用的weblogic7开发,有两种情况:
1。用的是weblogic标准的JMS ConnectionFactory,
编写的topic producer已经发布成功(console已经按照输出成功信息),但指定存放消息的目录并没有文件,运行同步消费者接收,没有任何提示和输出,也没有报错,好像一直在run,断掉weblogic server才停止。
2。用的试自己配的一个连接工厂和目录,用jb8集成weblogic7开发,用的Message-Driven Bean实现,deploy成功, 采用run as defaults,发布消息也成功,运行同步消费者接收bean时,一步一步的提示输出都已经输出,譬如:
start to receive
subscriber is created
connection  started
但到输出接收的消息仍然没有反应,一直在run
部分代码: 
   ...
   log("start to receive");//log 等同于system.out.println()

    subscriber=session.createSubscriber(newTopic);

    log("subscriber is created");

    m_topicConnection.start();

    log("connection started");
    //if synchronization

      TextMessage msg = (TextMessage) subscriber.receive();

      log("Received" + msg.getText());

      msg = (TextMessage) subscriber.receive();

      log("Received" + msg.getText());
 抛异常也试过,结果没变

高手指教啊,郁闷中

15 个解决方案

#1


第二个 消息显示在weblogic的控制台


http://expert.csdn.net/Expert/topic/1657/1657269.xml?temp=.6064264
http://expert.csdn.net/Expert/topic/1607/1607909.xml?temp=.2608454

#2


第二种情况我是直接点击JB8里面的“runing as default”,jb8窗口下放不就应该输出吗?
在console里面我也找过,好像没有啊?请告我在console那个里面

#3


我也遇到过 开始以为是程序错误 后来在weblogic的控制台看见了消息内容 就是那个dos窗口 仔细找找 可能夹在两行异常信息中间 

TextMessage msg = (TextMessage) subscriber.receive(); 不知道是否正确onMessage方法 改为如下试试

public void onMessage(Message message) {
    String msgText = "";
    String msgID = "";
    try {
      msgID = message.getJMSMessageID();
      if (message instanceof TextMessage) {
        msgText = ( (TextMessage) message).getText();
      }
      else {
        msgText = message.toString();
      }
    }
    catch (JMSException jmse) {
      jmse.printStackTrace();
    }
    System.out.println("There is a Message received:");
    System.out.println(msgText);
  }

#4


onMessage(Message message)方法已经有一个Message的参数 你还去receive 当然没有结果了

#5


补充 消息已经被消费掉了 Subscrib.rereceive当然收不到消息

#6


不对啊 我照着上面的方法(同步消费和异步消费)都做了,但还是接收不到消息,我把代码帖出来,大家帮忙看看:

package jmstest;

import javax.jms.*;
import javax.naming.*;
import java.util.Properties;

public class testLister implements MessageListener {

  public testLister() {
  }
  public static void main(String[] args) {
    testLister t1 = new testLister();
    try{
      t1.publish();
      t1.comsumer();
    }catch(Exception ex){
      ex.printStackTrace();
    }
  }
  public void publish() throws Exception{
    log("start publish.....");
    Context ctx = getInitialContext();
      TopicConnectionFactory tConFactory = (TopicConnectionFactory)ctx.lookup("MDBDemoCF");
      Topic newTopic = (Topic)ctx.lookup("MDBDemo Topic");
      TopicConnection tCon = tConFactory.createTopicConnection();
      TopicSession session = tCon.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
      TopicPublisher publisher = session.createPublisher(newTopic);
      TextMessage msg = session.createTextMessage();
      msg.setText("Hello");
      publisher.publish(msg);
      log("end publish.");
  }

  public void comsumer() throws Exception{
    log("start comsumer....");
      Context ctx = getInitialContext();
      TopicConnectionFactory tConFactory = (TopicConnectionFactory)ctx.lookup("MDBDemoCF");
      Topic newTopic = (Topic)ctx.lookup("MDBDemo Topic");
      TopicConnection tCon = tConFactory.createTopicConnection();
      TopicSession session = tCon.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
      TopicSubscriber subscriber = session.createSubscriber(newTopic);
      //异步
      //subscriber.setMessageListener(this);
      tCon.start();
      //同步
      TextMessage msg = (TextMessage)subscriber.receive();
      log("receiver="+msg.getText());
  }

  public void onMessage(Message message) {
    try{
    TextMessage msg = (TextMessage)message;
    System.out.println(msg.getText());
    }catch(Exception ex){
      ex.printStackTrace();
    }
  }

  private Context getInitialContext() throws NamingException {
  try {
    // Get an InitialContext
    Properties h = new Properties();
    h.put(Context.INITIAL_CONTEXT_FACTORY,
      "weblogic.jndi.WLInitialContextFactory");
    h.put(Context.PROVIDER_URL, "t3://localhost:7001");
    return new InitialContext(h);
  }catch (NamingException ex) {
    log("We were unable to get a connection to the WebLogic server at t3://localhost:7001");
    log("Please make sure that the server is running.");
    throw ex;
  }
  }

  private static void log(String s) {
    System.out.println(s);
  }

}

#7


好象消息发布者和消费者不能存在于一个类中 用嵌套类试试

#8


可以看看我的例子
http://expert.csdn.net/Expert/topic/1657/1657269.xml?temp=.6064264

#9


哦 我忘了一件事 消息发布是有一段延时 客户端不能马上收到 你做成非同步的持久订阅者
 public void comsumer() throws Exception {
    String clientID = "test";    
    log("start comsumer....");
    tCon = tConFactory.createTopicConnection();
    tCon.setClientID(clientID);
    session = tCon.createTopicSession(false,
                                      Session.AUTO_ACKNOWLEDGE);
    TopicSubscriber subscriber = session.createDurableSubscriber(newTopic,
        clientID);
    subscriber.setMessageListener(this);
    tCon.start();
  }

在main方法里多等会就能收到

public static void main(String[] args) {
    testLister t1 = new testLister();
    try {
      t1.publish();
      t1.comsumer();
      while (true) {
        Thread.sleep(500);
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

#10


是的, teafang(学习) 说的对,有两点要注意:
1、发布后最好等待一段时间再订阅
2、最好能异步接收,并且将MessageListener写在另一个类中
好象上面都提到了,这些我也是试了两天才得出的一点教训

下面有个例子,可以运行:
http://www.net7b.com/net7b_tech/lookart.asp?id={C4902551-DAB4-4251-808D-77DA15C63289}

#11


好象消息发布者和消费者不能存在于一个类中 用嵌套类试试  这个是我错了 忘了实现messagelistener接口了 你把改了的部分覆盖了就能接收了

#12


/*
 * @(#)DurableSubscriberExample.java 1.6 00/08/18
 * 
 * Copyright (c) 2000 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */
import javax.jms.*;

/**
 * The DurableSubscriberExample class demonstrates that a durable subscription
 * is active even when the subscriber is not active.
 * 


 * The program contains a DurableSubscriber class, a MultiplePublisher class, 
 * a main method, and a method that instantiates the classes and calls their
 * methods in sequence.
 * 


 * The program begins like any publish/subscribe program: the subscriber starts,
 * the publisher publishes some messages, and the subscriber receives them.
 * 


 * At this point the subscriber closes itself.  The publisher then publishes 
 * some messages while the subscriber is not active.  The subscriber then 
 * restarts and receives the messages.
 * 


 * Specify a topic name on the command line when you run the program.
 *
 * @author Kim Haase
 * @version 1.6, 08/18/00
 */
public class DurableSubscriberExample {
    String      topicName = null;
    int         exitResult = 0;
    static int  startindex = 0;

    /**
     * The DurableSubscriber class contains a constructor, a startSubscriber 
     * method, a closeSubscriber method, and a finish method.
     * 


     * The class fetches messages asynchronously, using a message listener, 
     * TextListener.
     *
     * @author Kim Haase
     * @version 1.6, 08/18/00
     */
    public class DurableSubscriber {
        TopicConnection  topicConnection = null;
        TopicSession     topicSession = null;
        Topic            topic = null;
        TopicSubscriber  topicSubscriber = null;
        TextListener     topicListener = null;

        /**
         * The TextListener class implements the MessageListener interface by 
         * defining an onMessage method for the DurableSubscriber class.
         *
         * @author Kim Haase
         * @version 1.6, 08/18/00
         */
        private class TextListener implements MessageListener {
            final SampleUtilities.DoneLatch  monitor =
                new SampleUtilities.DoneLatch();

            /**
             * Casts the message to a TextMessage and displays its text.
             * A non-text message is interpreted as the end of the message 
             * stream, and the message listener sets its monitor state to all 
             * done processing messages.
             *
             * @param message the incoming message
             */
            public void onMessage(Message message) {
                if (message instanceof TextMessage) {
                    TextMessage  msg = (TextMessage) message;
                    
                    try {
                        System.out.println("SUBSCRIBER: Reading message: " 
                                           + msg.getText());
                    } catch (JMSException e) {
                        System.out.println("Exception in onMessage(): " 
                                           + e.toString());
                    }
                } else {
                    monitor.allDone();
                }
            }
        }

        /**
         * Constructor: looks up a connection factory and topic and creates a 
         * connection and session.
         */
        public DurableSubscriber() {
            TopicConnectionFactory  topicConnectionFactory = null;

            try {
                topicConnectionFactory = 
                    SampleUtilities.getTopicConnectionFactory();
                topicConnection = 
                    topicConnectionFactory.createTopicConnection();
                topicConnection.setClientID("EE");
                topicSession = topicConnection.createTopicSession(false, 
                    Session.AUTO_ACKNOWLEDGE);
                topic = SampleUtilities.getTopic(topicName, topicSession);
            } catch (Exception e) {
                System.out.println("Connection problem: " + e.toString());
                if (topicConnection != null) {
                    try {
                        topicConnection.close();
                    } catch (JMSException ee) {}
                }
             System.exit(1);
            } 
        }

        /**
         * Stops connection, then creates durable subscriber, registers message 
         * listener (TextListener), and starts message delivery; listener
         * displays the messages obtained.
         */
        public void startSubscriber() {
            try {
                System.out.println("Starting subscriber");
                topicConnection.stop();
                topicSubscriber = topicSession.createDurableSubscriber(topic,
                    "MakeItLast");
                topicListener = new TextListener();
                topicSubscriber.setMessageListener(topicListener);
                topicConnection.start();
            } catch (JMSException e) {
                System.out.println("Exception occurred: " + e.toString());
                exitResult = 1;
            }
        }
        
        /**
         * Blocks until publisher issues a control message indica3ting
         * end of publish stream, then closes subscriber.
         */
        public void closeSubscriber() {
            try {
                topicListener.monitor.waitTillDone();
                System.out.println("Closing subscriber");
                topicSubscriber.close();
            } catch (JMSException e) {
                System.out.println("Exception occurred: " + e.toString());
                exitResult = 1;
            }
        }
        
        /**
         * Closes the connection.
         */
        public void finish() {
            if (topicConnection != null) {
                try {
                    topicSession.unsubscribe("MakeItLast");
                    topicConnection.close();
                } catch (JMSException e) {
                    exitResult = 1;
                }
            }
        }
    }

#13


/**
     * The MultiplePublisher class publishes several messages to a topic. It
     * contains a constructor, a publishMessages method, and a finish method.
     *
     * @author Kim Haase
     * @version 1.6, 08/18/00
     */
    public class MultiplePublisher {
        TopicConnection  topicConnection = null;
        TopicSession     topicSession = null;
        Topic            topic = null;
        TopicPublisher   topicPublisher = null;

        /**
         * Constructor: looks up a connection factory and topic and creates a 
         * connection and session.  Also creates the publisher.
         */
        public MultiplePublisher() {
            TopicConnectionFactory  topicConnectionFactory = null;

            try {
                topicConnectionFactory = 
                    SampleUtilities.getTopicConnectionFactory();
                topicConnection = 
                    topicConnectionFactory.createTopicConnection();
                topicSession = topicConnection.createTopicSession(false, 
                    Session.AUTO_ACKNOWLEDGE);
                topic = SampleUtilities.getTopic(topicName, topicSession);
                topicPublisher = topicSession.createPublisher(topic);
            } catch (Exception e) {
                System.out.println("Connection problem: " + e.toString());
                if (topicConnection != null) {
                    try {
                        topicConnection.close();
                    } catch (JMSException ee) {}
                }
             System.exit(1);
            } 
        }
        
        /**
         * Creates text message.
         * Sends some messages, varying text slightly.
         * Messages must be persistent.
         */
        public void publishMessages() {
            TextMessage   message = null;
            int           i;
            final int     NUMMSGS = 3;
            final String  MSG_TEXT = new String("Here is a message");

            try {
                message = topicSession.createTextMessage();
for (i = startindex; i < startindex + NUMMSGS; i++) {
                    message.setText(MSG_TEXT + " " + (i + 1));
//topicPublisher.publish( message, DeliveryMode.PERSISTENT,8, 0);

                    //System.out.println("PUBLISHER: Publishing message: " + message.getText());
                    //topicPublisher.publish(message);

                }
TextMessage message1 = topicSession.createTextMessage();
 message1.setText("The new Pro J2EE book is now out");
 topicPublisher.publish(message1, DeliveryMode.PERSISTENT,8, 0);
 //topicPublisher.publish(message1);
                // Send a non-text control message indicating end of messages.
                topicPublisher.publish(topicSession.createMessage());
                startindex = i;
            } catch (JMSException e) {
                System.out.println("Exception occurred: " + e.toString());
                exitResult = 1;
            }
        }
        
        /**
         * Closes the connection.
         */
        public void finish() {
            if (topicConnection != null) {
                try {
                    topicConnection.close();
                } catch (JMSException e) {
                    exitResult = 1;
                }
            }
        }
    }
    
    /**
     * Instantiates the subscriber and publisher classes.
     *
     * Starts the subscriber; the publisher publishes some messages.
     *
     * Closes the subscriber; while it is closed, the publisher publishes
     * some more messages.
     *
     * Restarts the subscriber and fetches the messages.
     *
     * Finally, closes the connections.    
     */
    public void run_program_r() {
        DurableSubscriber  durableSubscriber = new DurableSubscriber();
        durableSubscriber.startSubscriber();
durableSubscriber.closeSubscriber();
        durableSubscriber.finish();
    }
public void run_program_s() {
        MultiplePublisher  multiplePublisher = new MultiplePublisher();
        multiplePublisher.publishMessages();
multiplePublisher.finish();

    }
public void run_program() {
        MultiplePublisher  multiplePublisher = new MultiplePublisher();
DurableSubscriber  durableSubscriber = new DurableSubscriber();
        
durableSubscriber.startSubscriber();
multiplePublisher.publishMessages();
durableSubscriber.closeSubscriber();

        multiplePublisher.publishMessages();
durableSubscriber.startSubscriber();
durableSubscriber.closeSubscriber();

multiplePublisher.finish();
        durableSubscriber.finish();

    }
    /**
     * Reads the topic name from the command line, then calls the
     * run_program method.
     *
     * @param args the topic used by the example
     */
    public static void main(String[] args) {
        DurableSubscriberExample  dse = new DurableSubscriberExample();
        
        dse.topicName = "jms/WroxPublications";

if(args.length != 1){
dse.run_program();
}
else if(args[0].equals("r")){
dse.run_program_r();
}else if(args[0].equals("s")){
dse.run_program_s();
}
        
     SampleUtilities.exit(dse.exitResult);
    }
}

#14


大家看看上边的例子,

#15


注意不要在jbuilder8中指定连接工厂,指定了就接收不到

推荐阅读
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 解决java.lang.IllegalStateException: ApplicationEventMulticaster not initialized错误的方法和原因
    本文介绍了解决java.lang.IllegalStateException: ApplicationEventMulticaster not initialized错误的方法和原因。其中包括修改包名、解决service name重复、处理jar包冲突和添加maven依赖等解决方案。同时推荐了一个人工智能学习网站,该网站内容通俗易懂,风趣幽默,值得一看。 ... [详细]
  • 本文介绍了解决Netty拆包粘包问题的一种方法——使用特殊结束符。在通讯过程中,客户端和服务器协商定义一个特殊的分隔符号,只要没有发送分隔符号,就代表一条数据没有结束。文章还提供了服务端的示例代码。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
author-avatar
不会做饭的我
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有