基于Weblogic8的JMS队列实例配置

预计达成目标:Weblogic8下实现消息的异步传输与监听

 

为了实现不间断发送与监听,使用了Quartz自动调度,这个不是这篇文章重点,不做讲解,也可以将quartz去掉

quartz引用的jar包比较多,这里就不作为附件了,只上传jms需要依赖的jar包 ,其中还有weblogic.jar,太大了就没有上传,可以在weblogic的安装bin目录下找到. 

 

配置步骤

一、建立 Weblogic Domain:建立步骤不再累述

二、在Console控制台配置连接工厂与消息队列

      1、配置连接工厂 

        

  按如下路径点击:Service—>JMS—> Connection Factories,在右侧弹出的页面,点击链接Configure a new JMS Connection Factory...”,在新页面中输入NameJNDI Name此处两处均输入连接工厂名为:jms/connFactory,点击“Create则生成JMS连接工厂

    如下图:

    

 

 

 

       2、配置消息队列

      如下图位置配置队列,这个jmsServer是自己建立的

 

        

 

 

到此位置就配置了一个 名称为 jms/connFactory   连接工厂以及名为 queueasd 的队列

 

 

三、消息发送测试类

 

Java代码 复制代码  收藏代码
  1. package com.javasd.jms;   
  2.   
  3. import java.util.Properties;   
  4.   
  5. import javax.jms.BytesMessage;   
  6. import javax.jms.Queue;   
  7. import javax.jms.QueueConnection;   
  8. import javax.jms.QueueConnectionFactory;   
  9. import javax.jms.QueueSender;   
  10. import javax.jms.QueueSession;   
  11. import javax.jms.Session;   
  12. import javax.naming.Context;   
  13. import javax.naming.InitialContext;   
  14.   
  15. import org.quartz.CronTrigger;   
  16. import org.quartz.Job;   
  17. import org.quartz.JobDetail;   
  18. import org.quartz.JobExecutionContext;   
  19. import org.quartz.JobExecutionException;   
  20. import org.quartz.Scheduler;   
  21. import org.quartz.SchedulerFactory;   
  22. import org.quartz.impl.StdSchedulerFactory;   
  23.   
  24. public class TestJmsConnection implements Job {   
  25.        
  26.     public static void main(String args[]) throws Exception {   
  27.            
  28.         //quartz调度,不做讲解   
  29.         SchedulerFactory schedFact=new StdSchedulerFactory();   
  30.         Scheduler sched=schedFact.getScheduler();   
  31.         sched.start();   
  32.            
  33.            
  34.         JobDetail jobDetail=new JobDetail("a","b",TestJmsConnection.class);   
  35.         jobDetail.getJobDataMap().put("name","lucy");   
  36.           
  37.         CronTrigger trigger=new  CronTrigger("c","d");   
  38.         trigger.setCronExpression("0/100 * * * * ? " ); // 启动之后立即执行 每一秒继续重复。   
  39.         sched.scheduleJob(jobDetail, trigger);   
  40.                
  41.            
  42.     }   
  43.   
  44.     public void execute(JobExecutionContext arg0) throws JobExecutionException {   
  45.            
  46.         try{   
  47.                
  48.                
  49.             //这里才是jms发送端的逻辑   
  50.             Properties properties = new Properties();   
  51.             //设置连接属性   
  52.             //这个设置是固定的   
  53.             properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");   
  54.             //设置端口及IP 我的domain在本地,端口为9001   
  55.             properties.put(Context.PROVIDER_URL, "t3://localhost:9001");   
  56.             //与weblogic的console用户名密码一致   
  57.             properties.put(Context.SECURITY_PRINCIPAL, "weblogic");   
  58.             properties.put(Context.SECURITY_CREDENTIALS, "weblogic");   
  59.             //实例化上下文   
  60.             Context ctx = new InitialContext(properties);   
  61.                    
  62.                 //获取连接工厂   
  63.                 QueueConnectionFactory queueFactory = (QueueConnectionFactory)ctx.lookup("jms/connFactory");   
  64.                 //根据连接工厂获取连接   
  65.                 QueueConnection queueConn = queueFactory.createQueueConnection();   
  66.                 //根据连接获取操作的session实例   
  67.                 QueueSession qSession = queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);   
  68.                 //通过上下文jndi查找配置的队列,我配置的队列名称及jndi名称均为 queueasd   
  69.                 Queue queue = (Queue)ctx.lookup("queueasd");   
  70.                 //通过session以及指定的队列实例化消息发送器   
  71.                 QueueSender queueSender = qSession.createSender(queue);   
  72.                 //打开连接   
  73.                 queueConn.start();   
  74.                    
  75.                 //测试用要发送的字符串   
  76.                 String s = "just test sending of jms under weblogic8!";   
  77.                 //转换为byte数组   
  78.                 byte[] bytes = s.getBytes();   
  79.                 int byteLength = bytes.length;   
  80.                 //通过session的createBytesMessage方法实例化一个jms识别的消息实体对象   
  81.                 BytesMessage bytesMessage = qSession.createBytesMessage();    
  82.                 //为这个message对象设置值   
  83.                 //设置消息的长度   
  84.                 bytesMessage.writeInt(byteLength);   
  85.                 //设置消息的内容   
  86.                 bytesMessage.writeBytes(bytes);   
  87.                 //发送消息   
  88.                 queueSender.send(bytesMessage);   
  89.         } catch(Exception e ){   
  90.             e.printStackTrace();   
  91.         }   
  92.            
  93.            
  94.     }   
  95.        
  96.        
  97.        
  98. }  
package com.javasd.jms;

import java.util.Properties;

import javax.jms.BytesMessage;
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.naming.Context;
import javax.naming.InitialContext;

import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class TestJmsConnection implements Job {
	
	public static void main(String args[]) throws Exception {
		
		//quartz调度,不做讲解
		SchedulerFactory schedFact=new StdSchedulerFactory();
		Scheduler sched=schedFact.getScheduler();
		sched.start();
		
		
		JobDetail jobDetail=new JobDetail("a","b",TestJmsConnection.class);
        jobDetail.getJobDataMap().put("name","lucy");
       
        CronTrigger trigger=new  CronTrigger("c","d");
        trigger.setCronExpression("0/100 * * * * ? " ); // 启动之后立即执行 每一秒继续重复。
        sched.scheduleJob(jobDetail, trigger);
			
		
	}

	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		
		try{
			
			
			//这里才是jms发送端的逻辑
			Properties properties = new Properties();
			//设置连接属性
			//这个设置是固定的
			properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
			//设置端口及IP 我的domain在本地,端口为9001
			properties.put(Context.PROVIDER_URL, "t3://localhost:9001");
			//与weblogic的console用户名密码一致
			properties.put(Context.SECURITY_PRINCIPAL, "weblogic");
			properties.put(Context.SECURITY_CREDENTIALS, "weblogic");
			//实例化上下文
			Context ctx = new InitialContext(properties);
				
				//获取连接工厂
				QueueConnectionFactory queueFactory = (QueueConnectionFactory)ctx.lookup("jms/connFactory");
				//根据连接工厂获取连接
				QueueConnection queueConn = queueFactory.createQueueConnection();
				//根据连接获取操作的session实例
				QueueSession qSession = queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
				//通过上下文jndi查找配置的队列,我配置的队列名称及jndi名称均为 queueasd
				Queue queue = (Queue)ctx.lookup("queueasd");
				//通过session以及指定的队列实例化消息发送器
				QueueSender queueSender = qSession.createSender(queue);
				//打开连接
				queueConn.start();
				
				//测试用要发送的字符串
				String s = "just test sending of jms under weblogic8!";
				//转换为byte数组
				byte[] bytes = s.getBytes();
				int byteLength = bytes.length;
				//通过session的createBytesMessage方法实例化一个jms识别的消息实体对象
				BytesMessage bytesMessage = qSession.createBytesMessage(); 
				//为这个message对象设置值
				//设置消息的长度
				bytesMessage.writeInt(byteLength);
				//设置消息的内容
				bytesMessage.writeBytes(bytes);
				//发送消息
				queueSender.send(bytesMessage);
		} catch(Exception e ){
			e.printStackTrace();
		}
		
		
	}
	
	
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值