eg:新建SyConsumer类
/*同步接受消息*/ //先接收 后发送 等待消息队列里收到消息 public class SyConsumer { //1.获取命名服务的Context对象(获取初始化容器) private Context getInitalContext() throws Exception { final String INIT_FACTORY = "weblogic.jndi.WLInitialContextFactory"; final String SERVER_URL = "t3://localhost:7001"; Properties props = new Properties(); props.put(Context.INITIAL_CONTEXT_FACTORY, INIT_FACTORY); props.put(Context.PROVIDER_URL, SERVER_URL); Context ctx = new InitialContext(props); return ctx; } //2.同步接收PTP消息 public void reciveMessage() throws Exception { //定义默认的连接工厂 final String CONNECTION_FACTORY = "weblogic.jms.ConnectionFactory"; //获取容器 实例化 Context ctx = getInitalContext(); //查找连接对象 通过JNDI查找获取JMS连接工厂 ConnectionFactory connFactory = (ConnectionFactory) ctx.lookup(CONNECTION_FACTORY); //通过JNDI查找获取JMS消息目的 Destination dest = (Destination) ctx.lookup("MessageQueue"); //通过JMS工厂创建JMS连接 Connection conn = connFactory.createConnection(); //启动JMS连接,开始传输JMS消息 conn.start(); //JMS连接创建JMS会话 Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); //利用JMS会话创建消息消费者对象 MessageConsumer receiver = session.createConsumer(dest); //利用消息的消费者对象同步的接收消息 TextMessage msg = (TextMessage) receiver.receive(); System.out.println(msg); System.out.println("同步接收到的消息:"+msg.getText()); //释放资源 session.close(); conn.close(); } //3.测试执行 public static void main(String[] args) { // TODO Auto-generated method stub //new自身一个对象 SyConsumer sc = new SyConsumer(); try { sc.reciveMessage(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }eg:新建AsyncConSumer类实现MessageListener监听接口
public class AsyncConSumer implements MessageListener { //3.PTP异步接收消息 public AsyncConSumer() throws Exception { final String CONNECTION_FACTORY_JNDI = "weblogic.jms.ConnectionFactory"; Context ctx = getInitalContext(); ConnectionFactory connFactory = (ConnectionFactory) ctx.lookup(CONNECTION_FACTORY_JNDI); Connection conn = connFactory.createConnection(); Destination dest = (Destination) ctx.lookup("MessageQueue"); conn.start(); Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer receiver = session.createConsumer(dest); //设置一个消息的监听器 receiver.setMessageListener(this); //接收时让线程睡眠多长时间 10s Thread.sleep(10000); session.close(); conn.close(); } //1.获取命名服务的Context对象(获取初始化容器) private Context getInitalContext() throws Exception { final String INIT_FACTORY = "weblogic.jndi.WLInitialContextFactory"; final String SERVER_URL = "t3://localhost:7001"; Properties props = new Properties(); props.put(Context.INITIAL_CONTEXT_FACTORY, INIT_FACTORY); props.put(Context.PROVIDER_URL, SERVER_URL); Context ctx = new InitialContext(props); return ctx; } @Override//2.监听器 public void onMessage(Message msg) { // TODO Auto-generated method stub TextMessage message = (TextMessage) msg; System.out.println(message); try { System.out.println("异步接收到的消息"+message.getText()); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //4.测试 public static void main(String[] args) { // TODO Auto-generated method stub try { //由于构造方法 new的时候会初始化 AsyncConSumer consumer = new AsyncConSumer(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }