weblogic 向集群中发送消息

domain1:success  只有一个AdminServer:

项目部署如下:

domain2:example    有一个主管服务器、两个受管服务器和一个代理服务器(代理服务器暂时不用);

项目部署如下:

下面的分别是jms服务器和jms模块:


测试的sendQueue和receiveQueue代码如下:


package com.sender;

import java.util.Hashtable;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;


import date.Student;

public class SendQueue implements javax.jms.ExceptionListener{
	private QueueConnectionFactory factoryMain;    
	private QueueConnection        connectionMain; 
	private QueueSession           sessionMain;   
	private QueueSender            senderMain;     
	private static SendQueue    sendPnoQueue = null;
	
	
	//构造方法私有化
	private SendQueue() {
        //setStrArea(dmsArea);
        init();
    }
	
	//获取实例化 单例模式
	public static SendQueue getFactroySendPnoQueue()  throws Exception  {
        if(sendPnoQueue == null) {
            sendPnoQueue = new SendQueue();
        }
        return sendPnoQueue;
    }
	
	//初始化配置
	private boolean init() {
        boolean s = true;
        String main_jndi_calssname = "";
        String main_jms_con_ip = "";
        String main_factory_con = "";
        String main_pno_queue = "";
        try {
        	main_jndi_calssname = "weblogic.jndi.WLInitialContextFactory";
    		main_jms_con_ip = "t3://11.205.240.41:7101,11.205.240.41:7102";
    		main_factory_con = "ConnectionFactory-0";
    		main_pno_queue = "DistributedQueue-1";
        } catch (Exception e) {
            e.printStackTrace();
        }

        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, main_jndi_calssname);
        env.put(Context.PROVIDER_URL, main_jms_con_ip);
        try {

            // Look up administratered objects
            final Context initContext = new InitialContext(env);
            factoryMain = (QueueConnectionFactory) initContext.lookup(main_factory_con);
            Queue mainqueue = (Queue) initContext.lookup(main_pno_queue);
            initContext.close();
            // Create JMS objects
            connectionMain = factoryMain.createQueueConnection();
            sessionMain = connectionMain.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            connectionMain.setExceptionListener(this);
            senderMain = sessionMain.createSender(mainqueue);
            connectionMain.start();
        } catch (Exception e) {
            e.printStackTrace();
            s = false;
        }
        return s;

    }
	
	//发送入口
	public void dowork(Object str) throws Exception{
		sendMsg(str);
	}
	
	//往队列发送
	private void sendMsg(Object object) throws Exception{
		try {
			TextMessage message = sessionMain.createTextMessage();
			message.setText((String)object);
			/*ObjectMessage oMessage = sessionMain.createObjectMessage();
			oMessage.setObject(arg0).setObjectProperty("", object);
			if(object instanceof Student){
				senderMain.send(oMessage);
			}*/
			long sendtime = System.currentTimeMillis();
			message.setLongProperty("sendtime", sendtime);
			senderMain.send(message);
			System.out.println("发送到队列上了哦。。。");
            //senderMain.send(sessionMain.createTextMessage((String) object));
            
        } catch (JMSException e) {
            e.printStackTrace();
            throw new Exception("(((((((((((((((((((((((((((((((((((((((((((((((((("+e.getLocalizedMessage() );
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("(((((((((((((((((((((((((((((((((((((((((((((((((("+e.getLocalizedMessage());
        }
	}
	
	
	//初始化失败线程停止1秒再次初始化
	public void onException(JMSException arg0) {
		while (!init()) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
		// TODO Auto-generated method stub
		System.out.println("connectionMain.setExceptionListener(this))))))))))))))))))))))))))))))))))))))))))))))))));");
	}

    public static SendQueue getSendPnoQueue() {
		return sendPnoQueue;
	}

	public static void setSendPnoQueue(SendQueue sendPnoQueue) {
		SendQueue.sendPnoQueue = sendPnoQueue;
	}
}



package com.receiver;

import java.util.Hashtable;

import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

import date.Student;

public class ReceiveQueue implements MessageDrivenBean, ExceptionListener, MessageListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private MessageDrivenContext m_context;
	private QueueConnectionFactory factoryMain;   
	private QueueConnection        connectionMain; 
	private QueueSession           sessionMain;    
	private QueueReceiver            QueueReceiver;  
	private static ReceiveQueue receiveQueue = null;
	
	private ReceiveQueue(){
		init();
	}
	
	//单例模式
	public static ReceiveQueue getReceiveQueueFactroy(){
		if(receiveQueue == null){
			return new ReceiveQueue();
		}else {
			return receiveQueue;
		}
	}
	
	//初始化配置
	private boolean init() {
        boolean s = true;
        String main_jndi_calssname = "";
        String main_jms_con_ip = "";
        String main_factory_con = "";
        String main_pno_queue = "";
        try {
        	main_jndi_calssname = "weblogic.jndi.WLInitialContextFactory";
    		main_jms_con_ip = "t3://11.205.240.41:7101,11.205.240.41:7102";
    		main_factory_con = "ConnectionFactory-0";
    		main_pno_queue = "DistributedQueue-1";
        } catch (Exception e) {
            e.printStackTrace();
        }

        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, main_jndi_calssname);
        env.put(Context.PROVIDER_URL, main_jms_con_ip);
        try {

            // Look up administratered objects
            final Context initContext = new InitialContext(env);
            factoryMain = (QueueConnectionFactory) initContext.lookup(main_factory_con);
            Queue mainqueue = (Queue) initContext.lookup(main_pno_queue);
            initContext.close();
            // Create JMS objects
            connectionMain = factoryMain.createQueueConnection();
            sessionMain = connectionMain.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            connectionMain.setExceptionListener(this);
            QueueReceiver = sessionMain.createReceiver(mainqueue);
            QueueReceiver.setMessageListener(this);
            connectionMain.start();
            //senderMain = sessionMain.createSender(mainqueue);
        } catch (Exception e) {
            e.printStackTrace();
            s = false;
        }
        return s;

    }

	//异常监听
	public void onException(JMSException arg0) {
		// TODO Auto-generated method stub
		System.out.println("接收出错了!");
	}

	//监听程序
	public void onMessage(Message arg0) {
		// TODO Auto-generated method stub
		Student student = null;
		String text = null;
		 try {  
			System.out.println(arg0.getStringProperty("banben"));
			//System.out.println(arg0 instanceof Student);
		    if (arg0 instanceof ObjectMessage) {     
		            student = (Student)(((ObjectMessage) arg0).getObject());
		        }else if(arg0 instanceof TextMessage){
		        	text = ((TextMessage)arg0).getText();
		        }
		    if(student != null){
		    	System.out.println(student.getName());
		    	System.out.println(student.getAge());
		    }
		    if(text != null){
		    	System.out.println("从队列中去到的信息为"+text);
		    }
		} catch (Exception e) {  
		    // TODO Auto-generated catch block  
		    e.printStackTrace();  
		}     
		//System.out.println(msgText + " " + d); 
	}

	public void ejbRemove() throws EJBException {
		// TODO Auto-generated method stub
		
	}

	public void setMessageDrivenContext(MessageDrivenContext arg0)
			throws EJBException {
		// TODO Auto-generated method stub
		m_context = arg0;
	}
	
	public void ejbCreate(){
		
	}
	
	
}



package com;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import com.sender.SendQueue;


public class SendTest {
    
    //private static SendPnoQueue    sendPnoQueue = null;
	/**
	 * @param args
	 */
	public static void send() {
		try {
			SendQueue sendQueue = SendQueue.getFactroySendPnoQueue();
			// 第1步、使用File类找到一个文件
			File f= new File("F:\\MyEclipse2014\\Test2\\WebRoot\\config\\test.xml") ;	// 声明File对象
			// 第2步、通过子类实例化父类对象
			InputStream input = null ;	// 准备好一个输入的对象
			input = new FileInputStream(f)  ;	// 通过对象多态性,进行实例化
			// 第3步、进行读操作
			byte b[] = new byte[(int)f.length()] ;		// 数组大小由文件决定
			int len = input.read(b) ;		// 读取内容
			// 第4步、关闭输出流
			String xmlString = new String(b);
			//System.out.println(xmlString);
			input.close() ;	
			for(int n = 0;n<20;n++){
				sendQueue.dowork(xmlString);
				Thread.sleep(1000);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}



package com;

import com.receiver.ReceiveQueue;

public class ReceiverTest {
	public void receive(){
		ReceiveQueue.getReceiveQueueFactroy();
	}
}



以上代码就是部署到服务器中的部分主要功能代码;


当domain1:success  的主管服务器AdminServer向   domain2:example  集群中的受管服务器server1,server2发送test.xml文本后,

却只有server1收到消息:

server1 后台收到的消息如下:

但 server2却未能接受到消息。。。求大神解释(顺便把这个有关JMS发送消息及接受消息的代码共享给大家),不知是否是集群的配置哪错了。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值