JNDI解析
http://blog.csdn.net/zhaosg198312/article/details/3979435
我对于jndi的理解 java naming and directory interface
命名服务,一个值和另一个值的映射;如学号和姓名
目录服务,至于目录服务,从计算机角度理解为在互联网上有着各种各样的资源和主机,但是这些内容都是散落在互联网中,为了访问这些散落的资源并获得相应的服务,就需要用到目录服务.
容器提供jndi,jndi就相当于java应用程序合在一起的粘合剂,容器本身提供了jndi规范的实现
容器实现了jndi规范,将相应的配置和管理交给容器,设置好相应的jndi参数,我们只需要对这么配合进行引用即可。
使用jndi先要初始化一个initialContext,初始上下文,这个初始上下文可以使远程的也可以是本地的,创建之后就可以从这个上下文中lookup jndi的引用名称了从而获得你所需要的资源
关于topic和queue的区别详见http://www.cnblogs.com/javahuang/archive/2013/04/28/3048957.html
jms的api http://docs.oracle.com/javaee/1.3/jms/tutorial/1_3_1-fcs/doc/prog_model.html
Connection Factories
At the beginning of a JMS client program, you usually perform a JNDI API lookup of the connection factory ConnectionFactory一般由jndi创建
其中创建的factory有两种Each connection factory is an instance of either the QueueConnectionFactory or the TopicConnectionFactory interface
Connections
创建:QueueConnection queueConnection =queueConnectionFactory.createQueueConnection();
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
关闭:queueConnection.close();
topicConnection.close();
When an application completes, you need to close any connections that you have created. Failure to close a connection can cause resources not to be released by the JMS provider. Closing a connection also closes its sessions and their message producers and message consumers.
Sessions
A session is a single-threaded context for producing and consuming messages. You use sessions to create message producers, message consumers, and messages. Sessions serialize the execution of message listeners; --session是单线程上线文生产和消费message,可以用session创建消息生产,消费和消息 session序列
消息监听 messages listeners
A message listener is an object that acts as an asynchronous event handler for messages. This object implements the MessageListener interface, which contains one method, onMessage. In the onMessage method, you define the actions to be taken when a message arrives.—消息监听充当异步消息时间处理事件,实现了MessageListener 接口,包含一个方法onMessage(),可以定义一个消息到来时采取的行为
TopicSession topicSession =
topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Controlling Message Acknowledgment
topic可以一对多,queue是一对一
The javax.naming.InitialContext class implements the Context interface and serves as our entry point to a naming system. To use JNDI to access objects in a naming system, you must first create an InitialContextobject. The InitialContext constructor takes a set of properties, in the form of a java.util.Hashtable or one of its subclasses, such as a Properties object. Here is how we created an InitialContext in the Lookup example: |
jndi调用时,各种应用服务器InitialContext的写法
Hashtable properties = new Hashtable();
properties.put(
Context.INITIAL_CONTEXT_FACTORY,
"org.exolab.jms.jndi.InitialContextFactory");
//openJms默认的端口是1099
properties.put(Context.PROVIDER_URL,
"rmi://localhost:1099/");
Context context = new
InitialContext(properties);
调用ejb时,如果客户端和ejb不在同一个jvm,就要设置InitialContext,不同的应用服务器InitialContext写法也不同.
Context.INITIAL_CONTEXT_FACTORY:指定到目录服务的连接工厂
Context.PROVIDER_URL:目录服务提供者URL
//jboss:
Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"
Context.URL_PKG_PREFIXES, "org.jboss.naming"
Context.PROVIDER_URL, "localhost:1099"
//weblogic:
Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory"
Context.PROVIDER_URL, "t3://localhost:7001"
//apusic(金蝶):
Context.INITIAL_CONTEXT_FACTORY,
"com.apusic.jndi.InitialContextFactory"
Context.PROVIDER_URL, "rmi://localhost:6888"
//WebSphere:
Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory"
Context.PROVIDER_URL, "iiop://localhost:900"
//J2EE SDK(J2EE RI):
Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.cosnaming.CNCtxFactory"
Context.PROVIDER_URL, "iiop://127.0.0.1:1050"
//SilverStream:
Context.INITIAL_CONTEXT_FACTORY, "com.sssw.rt.jndi.AgInitCtxFactory"
Context.PROVIDER_URL, "sssw://localhost:80"
//OC4J:
Context.INITIAL_CONTEXT_FACTORY,
"com.evermind.server.rmi.RMIInitialContextFactory"
Context.PROVIDER_URL, "ormi://127.0.0.1/"
//WAS5:
Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory"
Context.PROVIDER_URL, "iiop://localhost:2809"
常用JNDI服务提供者连接工厂:
Filesystem: Com.sun.jndi.fscontext.FSContextFactory或者com.sun.jndi.fscontext.RefFSContextFactory
LDAPv3: Com.sun.jndi.ldap.LdapCtxFactory
NDS: com.novell.naming.service.nds.NdsInitialContextFactory
NIS: com.sun.jndi.nis.NISCtxFactory
RMI registry: com.sun.jndi.rmi.registry.RegistryContextFactory
IBM LDAP服务提供者: com.ibm.jndi.LDAPCtxFactory
BEA 名字服务提供者: weblogic.jndi.WLInitialContextFactory
JBOSS名字服务提供者: org.jnp.interfaces.NamingContextFactory
评论
# 补充Borland Enterprise Server JNDI用法[未登录] 2008-12-20 23:08 James
Properties props=new Properties();
props.put(Context.PROVIDER_URL,"corbaloc::173.6.7.143:14500/NameService");
props.put("java.naming.factory.initial","com.inprise.j2ee.jndi.CtxFactory");
Context context = new InitialContext(props);
// Context context = new InitialContext();
Object ref = context.lookup("com/borland/examples/j2ee/hello/Hello");
HelloWorldHome home = (HelloWorldHome)
javax.rmi.PortableRemoteObject.narrow(ref, HelloWorldHome.class);
hello = home.create();
re: jndi调用时,各种应用服务器InitialContext的写法 2010-07-12 17:01
jndi调用时,各种应用服务器InitialContext的写法
websphere容器中spring对datasource的集成
|
三.JMS和事务
JMS提供两种事务控制方式,使用事务性的session,在JTA全局事务中使用JMS。
1消息生产者发送的消息会被缓存,在事务被提交之前,消息消费者不会接受到
任何未提交的消息,当消息生产者完成一次业务逻辑之后,消息生产者执行提交
事务,之前所有发送的消息才会被整体性地传递到消息消费者,如果事务回滚,
JMS服务器会直接丢弃所有缓存的消息。而对于消息消费者,在接收到并处理多
个消息成功之后,消息消费者提交事务,此时才会向消息生产者确认之前收到的
所有消息。如果事务回滚,JMS服务器会把所有消息退还给相关的消息队列和消
息主题。这里要注意,使用事务性session时,只是控制有关JMS的操作成为一个
整体,而如果要把有关EJB的操作,数据库的操作和JMS的操作都看成是一个整体
的话,我们就要使用JTA全局事务。
2.JTA全局事务,
什么是JTA,JTA,即Java Transaction API,译为Java事务API。我的理解是,
JTA允许两个或多个网络计算机上的java平台的组件参与到一个JTA事务中。
在JMS中如何使用?通过JNDI查找来获得JTA服务器的引用。如:
UserTransaction tx = (UserTransaction)ctx.lookup("javax.transaction.UserTransaction");
tx.begin();
...
tx.commit();