weblogic rmi编程,提供一个案例
时间: 2024-05-14 22:15:55 浏览: 10
好的,关于weblogic rmi编程的案例,以下是一个简单的示例:
服务端代码:
```
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import weblogic.jndi.WLInitialContextFactory;
public class RmiServer {
public static void main(String[] args) throws NamingException {
String contextFactory = "weblogic.jndi.WLInitialContextFactory";
String port = "7001";
String serverUrl = "t3://localhost:" + port;
String jndiName = "MyService";
Context context = new InitialContext();
context.addToEnvironment(Context.INITIAL_CONTEXT_FACTORY, contextFactory);
context.addToEnvironment(Context.PROVIDER_URL, serverUrl);
MyServiceImpl serviceImpl = new MyServiceImpl();
context.rebind(jndiName, serviceImpl);
System.out.println("Service started successfully...");
}
}
```
客户端代码:
```
import java.rmi.RemoteException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import weblogic.jndi.WLInitialContextFactory;
public class RmiClient {
public static void main(String[] args) throws NamingException, RemoteException {
String contextFactory = "weblogic.jndi.WLInitialContextFactory";
String port = "7001";
String serverUrl = "t3://localhost:" + port;
String jndiName = "MyService";
Context context = new InitialContext();
context.addToEnvironment(Context.INITIAL_CONTEXT_FACTORY, contextFactory);
context.addToEnvironment(Context.PROVIDER_URL, serverUrl);
MyService myService = (MyService) PortableRemoteObject.narrow(
context.lookup(jndiName),
MyService.class
);
System.out.println(myService.sayHello("WebLogic RMI"));
}
}
```
MyServiceImpl.java:
```
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class MyServiceImpl extends UnicastRemoteObject implements MyService {
protected MyServiceImpl() throws RemoteException {
super();
}
@Override
public String sayHello(String name) throws RemoteException {
return "Hello, " + name;
}
}
```
MyService.java:
```
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface MyService extends Remote {
public String sayHello(String name) throws RemoteException;
}
```
这是一个基本的 WebLogic RMI 编程示例。 服务器将一个实现MyService接口的远程对象公开给客户端,然后客户端通过JNDI查找当前正在运行的服务器上的对象。客户端然后调用该对象的方法,并使用返回结果。