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();