weblogic之高级进阶JMS的应用
不仅Activemq提供的jms服务,Weblogic也提供了jms服务,很多项目是部署在Weblogic中,使用其提供的jms服务也是很多企业的选择,一直没亲手试过,今天试了下,遇到的小问题还挺多,看来眼过百变不如手过一遍,下面就记录下操作步骤。构建完成后还会实现个小例子来测试下,这里主要进行点对点模式构建。
二. JMS的思想
所谓的JMS其实就是异步通信, 我们可以打个简单的比方: 现在的手机基本上普及了, 手机它有两个基本功能, 一个是打电话, 一个是发短信.
打电话是同步的, 必须要保证双方都开机才能进行通话; 而发短信则是异步的, 接收方不需要保持开机状态;
SUN公司给我们提供了一组标准被Java API用于企业级的消息处理, 通过JMS可以在Java程序之间发送和接受消息以达到交换数据的目的,
异步通信实现了程序之间的松耦合的关系.
Weblogic构建jms服务基本都是下面四个步骤(假设域已建好):
1. 新建jms服务器
2. 新建jms模块
3. 新建子部署
4. 新建资源
首先要登录Weblogic控制台(版本为weblogic 10),http://localhost:9101/console/ --------------------- 本文来自 琉璃糖 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/yuanxiaojiao0012/article/details/50685835?utm_source=copy
第一步要明白weblogic提供的jms的架构体系,通过一个简单的demo进行演示

要在AdminServer或者被管服务上创建JMS Server,。JmsServer中部署的是Jms module,在jms module可以创建异步消息的qunue和topic,以及外部访问的连接工厂,客户端是通过jndi容器的方式访问queue等对象的,所以在创建queue要指定jndi的唯一标识。
Weblogic作为Jms服务端提供Jms服务需要在Weblogic的控制台进行一些配置,主要是Jms服务器和Jms模块。Jms服务器作为Jms服务的核心提供Jms服务,也可以说是作为Jms模块的一个管理容器;Jms模块下可以进行ConnectionFactory、Topic、Queue等资源的定义,Weblogic允许我们通过JNDI对它们进行访问。
在安装的weblogic的时候再D:\Oracle\Middleware\wlserver_10.3\samples\server\examples\src\examples\jms目录下提供的jms的案例。我们就基于该案例来配置我们的weblogic的jms
一、新建jms服务器
左侧域结构->服务->消息传送->jms服务器

点击【新建】,弹出下面页面,开始填写信息,这里给jms服务器起名为JMSServer-0(默认的)

点击【下一步】,目标那选择当前服务器(AdminServer) ,将jms Server部署到AdminServer中
点击完成

点击完成
二、新建jms模块
早期weblogic可以不用构建jms模块直接新建jms连接工厂,队列等,新版本采用jms模块来组织jms连接工厂等。
左侧域结构->服务->消息传送->jms模块

点击【新建】按钮,填写名称,这里为MyJMSModule

点击【下一步】,勾选要部署的服务器AdminServer

点击【下一步】,这个页面中有一项“是否要向此jms模块中添加资源”,这个勾不勾选都可以,勾选的话在完成jms模块配置后可以立即配置资源(连接工厂,队列等),不勾选也可以稍后再配。勾选后进人下面界面:


在资源中主要是添加连接工厂、queue和topic三个资源
这里补充下分布式队列和分布式topic

不同的queue组成一个集群,形成一个分布式的队列,消息发布是发送到分布式队列中,具体是左边的queue还是右边的queue处理,由weblogic来决定,实现一个负载均衡的效果

下一步:我们新建一个连接工厂的资源
点击下一步:

点击下一步:
在创建的连接工厂是通过jndi容器提供的外部访问的,这里在创建的时候需要制定一个唯一的标志,在上面的jms 案例中使用的标志是
在D:\Oracle\Middleware\wlserver_10.3\samples\server\examples\src\examples\jms\queue案例中QueueSend.java中制定了标志的名称是
// Defines the JMS context factory.
public final static String JMS_FACTORY="weblogic.examples.jms.QueueConnectionFactory";
所以我们在配置的时候,需要将连接工厂的jndi标志设置为weblogic.examples.jms.QueueConnectionFactory

接下来点击下一步,点击完成
四:创建消息队列

点击下一步:同理这里需要制定队列jndi的标志在QueueSend.java
// Defines the queue.
public final static String QUEUE="weblogic.examples.jms.exampleQueue";

点击下一步:接下来需要将队列部署到jms module中,需要创建一个子部署

点击新建子部署

点击确定

点击完成
配置完成之后:jms module的配置如下所示

这里我们就已经把jms配置完成了,我们就可以运行案例了
首先在eclipse上创建两个java工程

QueueSend.java代码如下:
package examples.jms.queue; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException; /** This example shows how to establish a connection
* and send messages to the JMS queue. The classes in this
* package operate on the same JMS queue. Run the classes together to
* witness messages being sent and received, and to browse the queue
* for messages. The class is used to send messages to the queue.
*
* @author Copyright (c) 1999,2011, Oracle and/or its affiliates. All Rights Reserved.
*/
public class QueueSend
{
// Defines the JNDI context factory.
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory"; // Defines the JMS context factory.
public final static String JMS_FACTORY="weblogic.examples.jms.QueueConnectionFactory"; // Defines the queue.
public final static String QUEUE="weblogic.examples.jms.exampleQueue"; private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueSender qsender;
private Queue queue;
private TextMessage msg; /**
* Creates all the necessary objects for sending
* messages to a JMS queue.
*
* @param ctx JNDI initial context
* @param queueName name of queue
* @exception NamingException if operation cannot be performed
* @exception JMSException if JMS fails to initialize due to internal error
*/
public void init(Context ctx, String queueName)
throws NamingException, JMSException
{
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();
} /**
* Sends a message to a JMS queue.
*
* @param message message to be sent
* @exception JMSException if JMS fails to send message due to internal error
*/
public void send(String message) throws JMSException {
msg.setText(message);
qsender.send(msg);
} /**
* Closes JMS objects.
* @exception JMSException if JMS fails to close objects due to internal error
*/
public void close() throws JMSException {
qsender.close();
qsession.close();
qcon.close();
}
/** main() method.
*
* @param args WebLogic Server URL
* @exception Exception if operation fails
*/
public static void main(String[] args) throws Exception { InitialContext ic = getInitialContext("t3://localhost:7001");
QueueSend qs = new QueueSend();
qs.init(ic, QUEUE);
readAndSend(qs);
qs.close();
} private static void readAndSend(QueueSend qs)
throws IOException, JMSException
{
BufferedReader msgStream = new BufferedReader(new InputStreamReader(System.in));
String line=null;
boolean quitNow = false;
do {
System.out.print("Enter message (\"quit\" to quit): \n");
line = msgStream.readLine();
if (line != null && line.trim().length() != 0) {
qs.send(line);
System.out.println("JMS Message Sent: "+line+"\n");
quitNow = line.equalsIgnoreCase("quit");
}
} while (! quitNow); } private static InitialContext getInitialContext(String url)
throws NamingException
{
Hashtable<String,String> env = new Hashtable<String,String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
} }
QueueReceive.java
package examples.jms.queue; import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException; /**
* This example shows how to establish a connection to
* and receive messages from a JMS queue. The classes in this
* package operate on the same JMS queue. Run the classes together to
* witness messages being sent and received, and to browse the queue
* for messages. This class is used to receive and remove messages
* from the queue.
*
* @author Copyright (c) 1999,2011, Oracle and/or its affiliates. All Rights Reserved.
*/
public class QueueReceive implements MessageListener
{
// Defines the JNDI context factory.
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory"; // Defines the JMS connection factory for the queue.
public final static String JMS_FACTORY="weblogic.examples.jms.QueueConnectionFactory"; // Defines the queue.
public final static String QUEUE="weblogic.examples.jms.exampleQueue"; private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueReceiver qreceiver;
private Queue queue;
private boolean quit = false; /**
* Message listener interface.
* @param msg message
*/
public void onMessage(Message msg)
{
try {
String msgText;
if (msg instanceof TextMessage) {
msgText = ((TextMessage)msg).getText();
} else {
msgText = msg.toString();
} System.out.println("Message Received: "+ msgText ); if (msgText.equalsIgnoreCase("quit")) {
synchronized(this) {
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
} catch (JMSException jmse) {
System.err.println("An exception occurred: "+jmse.getMessage());
}
} /**
* Creates all the necessary objects for receiving
* messages from a JMS queue.
*
* @param ctx JNDI initial context
* @param queueName name of queue
* @exception NamingException if operation cannot be performed
* @exception JMSException if JMS fails to initialize due to internal error
*/
public void init(Context ctx, String queueName)
throws NamingException, JMSException
{
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qreceiver = qsession.createReceiver(queue);
qreceiver.setMessageListener(this);
qcon.start();
} /**
* Closes JMS objects.
* @exception JMSException if JMS fails to close objects due to internal error
*/
public void close()throws JMSException
{
qreceiver.close();
qsession.close();
qcon.close();
}
/**
* main() method.
*
* @param args WebLogic Server URL
* @exception Exception if execution fails
*/ public static void main(String[] args) throws Exception { InitialContext ic = getInitialContext("t3://localhost:7001");
QueueReceive qr = new QueueReceive();
qr.init(ic, QUEUE); System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message)."); // Wait until a "quit" message has been received.
synchronized(qr) {
while (! qr.quit) {
try {
qr.wait();
} catch (InterruptedException ie) {}
}
}
qr.close();
} private static InitialContext getInitialContext(String url)
throws NamingException
{
Hashtable<String,String> env = new Hashtable<String,String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
} }
将两个java程序运行起来:就可以发送和接收消息了
运行起来之后,就可以监听jms的参数和消息设置了

接下来我们来监听weblogic的jms
第一个就是jms消息的持久化存储,消息可以存储在file中也可以存储在jdbc中,在jms Server的持久化存储中进行配置

还可以设置发生消息的最大字节数等

还可以查看消息的发送状态等信息

jms的持久化存储,把消息保存到文件中
需要新建一个持久化的存储



在jms Server配置中选择刚刚创建的文件持久化存储

weblogic之高级进阶JMS的应用的更多相关文章
- 潭州学院-JavaVIP的Javascript的高级进阶-KeKe老师
潭州学院-JavaVIP的Javascript的高级进阶-KeKe老师 讲的不错,可以学习 下面是教程的目录截图: 下载地址:http://www.fu83.cn/thread-283-1-1.htm ...
- C#可扩展编程之MEF学习笔记(五):MEF高级进阶
好久没有写博客了,今天抽空继续写MEF系列的文章.有园友提出这种系列的文章要做个目录,看起来方便,所以就抽空做了一个,放到每篇文章的最后. 前面四篇讲了MEF的基础知识,学完了前四篇,MEF中比较常用 ...
- 高级进阶DB2(第2版)——内部结构、高级管理与问题诊断
<高级进阶DB2(第2版)——内部结构.高级管理与问题诊断> 基本信息 作者: 牛新庄 出版社:清华大学出版社 ISBN:9787302323839 上架时间:2013-7-3 出版 ...
- MEF高级进阶
MEF高级进阶 好久没有写博客了,今天抽空继续写MEF系列的文章.有园友提出这种系列的文章要做个目录,看起来方便,所以就抽空做了一个,放到每篇文章的最后. 前面四篇讲了MEF的基础知识,学完了前四 ...
- .Net高级进阶,在复杂的业务逻辑下,如何以最简练的代码,最直观的编写事务代码?
本文将通过场景例子演示,来通俗易懂的讲解在复杂的业务逻辑下,如何以最简练的代码,最直观的编写事务代码. 通过一系列优化最终达到两个效果,1.通过代码块来控制事务(分布式事务),2.通过委托优化Tran ...
- 《Android高级进阶》读书笔记
<Android高级进阶>是据我所知的市面上唯一一本技术工具书,比较的高大全,作者的目的是为了对全领域有个初步的概念 No1: 在Android系统中,拥有事件传递处理能力的类有以下三种 ...
- Git log diff config高级进阶
Git 历史相关和 git config 高级进阶 前一段时间分享了一篇<更好的 git log>简要介绍怎么美化 git log 命令,其中提到了 alias命令,今天再继续谈谈 git ...
- [总]Android高级进阶之路
个人Android高级进阶之路,目前按照这个目录执行,执行完毕再做扩展!!!!! 一.View的绘制 1)setContentView()的源码分析 2)SnackBar的源码分析 3)利用decor ...
- 高级进阶DB2(第2版)
<高级进阶DB2(第2版)> 基本信息 作者: 牛新庄 出版社:清华大学出版社 ISBN:9787302323839 上架时间:2013-7-3 出版日期:2013 年7月 开本:16开 ...
随机推荐
- [站点推荐]001.学习新技能的37个最佳网站(The 37 Best Websites To Learn Something New)
忘了过于褒奖的学校.整天呆在拥挤的教室而效果却差得可怜.这些网站和应用涵盖了科学.艺术和技术的无数话题.它们可以教会你实践练习任何技能,从制作豆 沙到用 node.js 开发 app,而且它们都是免费 ...
- windows核心编程课程实践---多线程文件搜索器(MFC界面)
课上完了连老师见都没见一面QAQ....记录一下该小项目 效果如下: 1.实现文件搜索功能,并封装为类 1)首先是文件搜索类Rapidfinder的构造函数和析构函数和文件信息初始化函数和文件路径规格 ...
- 报错:The server cannot be started because one or more of the ports are invalid. Open the server editor and correct the invalid ports.
今天重装eclipse和Tomcat,启动时候报标题错“The server cannot be started because one or more of the ports are invali ...
- Vue中控制更新的方式
一. 强制更新的实例方法 vm.$forceUpdate() 迫使 Vue 实例重新渲染.注意它仅仅影响实例本身和插入插槽内容的子组件,而不是所有子组件. 在没有留意到数组或对象的变更检测注意事 ...
- python通用数据库操作工具 pydbclib
pydbclib是一个通用的python关系型数据库操作工具包,使用统一的接口操作各种关系型数据库(如 oracle.mysql.postgres.hive.impala等)进行增删改查,它是对各个p ...
- Windows平台搭建Git服务教程详解
引言 软件企业的核心就是代码,如何确保代码的安全?如何在团队开发中协同工作?为解决这些问题,我们需要采用相应的管理工具来满足管理的需求.探长从最初的VSS.SVN.TFS到现在的Git存储一路走来,感 ...
- Rocket - util - Counters
https://mp.weixin.qq.com/s/q7R2Dn9p9cch_ABN4raReQ 介绍几种计数器的实现,以及其中的一点小细节. 1. ZCounter ...
- Shell脚本 (三) 条件判断 与 流程控制
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 六.条件判断 1.基本语法 [ condition ](注意condition 前后要有空格) 注意:条 ...
- MyBatis(二)参数传递和自定义结果集
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.myBatis的注解使用方式 package com.webcode.mapper; import ...
- ASP.NET给图片自动添加水印
先建一个类,感觉注释已经很详细了,有不懂的欢迎评论 using System; using System.Collections.Generic; using System.Drawing; usin ...