JNDI demo
所属分类 java
浏览量 974
JNDI
Java Naming and Directory Interface
Pet.java
JndiDemo.java
Pet.java
import java.io.Serializable;
import java.rmi.Remote;
// 使用RMI实现JNDI对象绑定到服务器上,继承Remote
public class Pet implements Remote, Serializable {
private static final long serialVersionUID = 1L;
public int id;
public String name;
@Override
public String toString() {
return id+","+name;
}
}
JndiDemo.java
import java.rmi.registry.LocateRegistry;
import javax.naming.Context;
import javax.naming.InitialContext;
// Java Naming and Directory Interface
public class JndiDemo {
public static void bindPet() throws Exception{
LocateRegistry.createRegistry(6000);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
System.setProperty(Context.PROVIDER_URL, "rmi://localhost:6000");
InitialContext ctx = new InitialContext();
Pet pet = new Pet();
pet.id=1;
pet.name = "tiger";
ctx.bind("pet", pet);
ctx.close();
}
public static void findPet() throws Exception{
InitialContext ctx = new InitialContext();
Pet pet = (Pet) ctx.lookup("pet");
System.out.println(pet.toString());
ctx.close();
}
public static void main(String[] args) throws Exception {
bindPet();
findPet();
// netstat -an|grep 6000
// Thread.sleep(60000);
}
}
完整代码
https://gitee.com/dyyx/hellocode/blob/master/src/jndi/JndiDemo.java
上一篇
下一篇
nginx常用优化
JFR实战
log4j2使用要点
logback简介
MySQL获取所有表和表结构信息
BI常用词汇