关于rmi-iiop和jndi的问题

guaiguai506 2003-12-30 06:54:52
我在学习rmi-iiop和jndi中碰到问题,请高手帮忙看一下
有四个文件
IPKGenerator.java:
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface IPKGenerator extends Remote{
public long generate()throws RemoteException;
}

PKGenerator.java:
import java.rmi.RemoteException;
import javax.rmi.PortableRemoteObject;
public class PKGenerator extends PortableRemoteObject implements IPKGenerator{
public PKGenerator()throws Exception,RemoteException{
super();
}
public synchronized long generate()throws RemoteException{
return i++;
}
private static long i=System.currentTimeMillis();
}

Startup.java:
import javax.naming.*;
public class Startup{
public static void main(String[] args)throws Exception{
PKGenerator generator=new PKGenerator();
Context ctx=new InitialContext(System.getProperties());
ctx.rebind("PKGenerator",generator);
System.out.println("PKGenerator bound to JNDI tree.");
synchronized(generator){
generator.wait();
}
}
}

Client.java:
import javax.naming.*;
import java.rmi.*;
public class Client{
public static void main(String[] args)throws Exception{
Context ctx=new InitialContext(System.getProperties());
Object remoteObject=ctx.lookup("PKGenerator");
IPKGenerator generator=(IPKGenerator)javax.rmi.PortableRemoteObject.narrow(remoteObject,IPKGenerator.class);
System.err.println(generator.generate());
}
}

我用rmic编译了PKGenerator生成了根程序和程序框架,在运行Startup.java时抛出了NoInitialContextException异常,那位高手能告诉我该怎么样编译和执行这几个文件,小弟我刚学j2ee,请多指点,希望能尽量详细点,谢谢!
...全文
100 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
nicolas2 2004-01-02
  • 打赏
  • 举报
回复
在你的本机1050端口上开一个名字服务啊,这是一个不会中止的进程,地位和各位老大提到的什么服务器一样啊。
guaiguai506 2004-01-01
  • 打赏
  • 举报
回复
orbd -ORBInitialPort 1050 事什么意思呀,能不能解释一下呀,谢谢
nicolas2 2003-12-31
  • 打赏
  • 举报
回复
还忘了一件事,jdk1.4才可以
nicolas2 2003-12-31
  • 打赏
  • 举报
回复
initialNamingContext.rebind("generator
",generator);考,太匆忙!
客户端的初始化也一样。
nicolas2 2003-12-31
  • 打赏
  • 举报
回复
用这个
orbd -ORBInitialPort 1050
Properties prop = new Properties();
Properties contextprop = new Properties(); contextprop.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.cosnaming.CNCtxFactory");
contextprop.put(Context.PROVIDER_URL,"iiop://yourip:1050");

Context initialNamingContext = new InitialContext(contextprop);
initialNamingContext.rebind("",);
注意用rmic -iiop generator
才是iiop协议哦。
guaiguai506 2003-12-30
  • 打赏
  • 举报
回复
louisqiang(tenwin):
你说的启动服务器时说的什么服务器呀,我只有j2sdkee1.3,这个可以吗,该怎么样配置呀,我刚学习j2ee,不是太懂,请多多帮忙
neary 2003-12-30
  • 打赏
  • 举报
回复
学习
louisqiang 2003-12-30
  • 打赏
  • 举报
回复
我一个例子,你可以试一试:
四个文件:[代码在后]
Product.java
ProductImpl.java
ProductServer.java
ProductClient.java
步骤:
1.正常编译四个文件。
2.运行rmic -v1.2 *** 生成代理文件***_stub.class。该文件必须放置在客户端。
3.运行rmiregistry。
4.启动服务器。
5.运行客户端。

//***************************
//Product.java
import java.rmi.*;
public interface Product extends Remote
{
String getDescription() throws RemoteException;
}
//********************************
//ProductImpl.java
import java.rmi.*;
import java.rmi.server.*;


public class ProductImpl extends UnicastRemoteObject implements Product
{
public ProductImpl(String n) throws RemoteException
{
name = n;
}

public String getDescription() throws RemoteException
{
return "I am a " + name + ". Buy me!";
}

private String name;
}
//*********************************
//ProductServer.java
import java.rmi.*;
import java.rmi.server.*;

public class ProductServer
{
public static void main(String args[])
{
try
{
System.out.println("Constructing server implementations...");
ProductImpl p1 = new ProductImpl("Blackwell Toaster");
ProductImpl p2 = new ProductImpl("ZapXpress Microwave Oven");
System.out.println("Binding server implementations to registry...");

Naming.rebind("toaster", p1);
Naming.rebind("microwave", p2);

System.out.println("Waiting for invocations from clients...");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//******************************8
ProductClient.java
import java.rmi.*;
import java.rmi.server.*;

public class ProductClient
{
public static void main(String[] args)
{
System.setProperty("java.security.policy", "client.policy");
System.setSecurityManager(new RMISecurityManager());
String url = "rmi://localhost/";

try
{
Product c1 = (Product)Naming.lookup(url + "toaster");
Product c2 = (Product)Naming.lookup(url + "microwave");
System.out.println(c1.getDescription());
System.out.println(c2.getDescription());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
dongdong2112 2003-12-30
  • 打赏
  • 举报
回复
不同的服务器的Properties,不同如weblogic的
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, "t3://localhost:7001");
guaiguai506 2003-12-30
  • 打赏
  • 举报
回复
请问那该怎么样配置我的properties呢
noratong 2003-12-30
  • 打赏
  • 举报
回复
呵呵,有人回答你的问题了咧。

我想原因可能就是我说的那个jdk自带的命名服务不管用,非得用一个专门的J2ee服务器来测试你的这个代码吧。

不过楼上说的那个配置服务器的相应的Properties那又怎么配置咧???
还需要高手详细指点一下。
dongdong2112 2003-12-30
  • 打赏
  • 举报
回复
public class Startup{
public static void main(String[] args)throws Exception{
PKGenerator generator=new PKGenerator();
Context ctx=new InitialContext(System.getProperties());//这句有问题,要配置你的服务器对应的Properties
ctx.rebind("PKGenerator",generator);
System.out.println("PKGenerator bound to JNDI tree.");
synchronized(generator){
generator.wait();
}
}
}


由于你的JNDI的上下文没有创建起来,所以后面的工作都不能进行了
JDKTM 6 Documentation Legal Notices API, Language, and VM Specs Features Guides Release Notes Tool Docs Tutorials and Training JavaTM SE 6 Platform at a Glance This document covers the JavaTM Platform, Standard Edition 6 JDK. Its product version number is 6 and developer version number is 1.6.0, as described in Platform Name and Version Numbers. For information on a feature of the JDK, click on a component in the diagram below. JDK Java Language Java Language Tools & Tool APIs java javac javadoc apt jar javap JPDA jconsole Security Int'l RMI IDL Deploy Monitoring Troubleshoot Scripting JVM TI JRE Deployment Technologies Deployment Java Web Start Java Plug-in User Interface Toolkits AWT Swing Java 2D Accessibility Drag n Drop Input Methods Image I/O Print Service Sound Java SE API Integration Libraries IDL JDBCTM JNDITM RMI RMI-IIOP Scripting Other Base Libraries Beans Intl Support I/O JMX JNI Math Networking Override Mechanism Security Serialization Extension Mechanism XML JAXP lang and util Base Libraries lang and util Collections Concurrency Utilities JAR Logging Management Preferences API Ref Objects Reflection Regular Expressions Versioning Zip Instrument Java Virtual Machine Java HotspotTM Client VM Java HotspotTM Server VM Platforms SolarisTM Linux Windows Other -------------------------------------------------------------------------------- Release Notes Topics include New Features, Known Issues, Compatibility with Prior Releases, Supported System Configurations, Installation, and More) -------------------------------------------------------------------------------- API, Language, and Virtual Machine Documentation Java Platform API Specification (NO FRAMES) (included in the JDK documentation bundle and on java.sun.com ) Note About sun.* Packages The Java Language Specification (DOWNLOAD) The Java Virtual Machine Specification (DOWNLOAD) -------------------------------------------------------------------------------- Features Reference Guides - Java Platform All guides listed are included in the documentation download bundle as well as the java.sun.com website (unless otherwise noted). Java SE 6 Overview New Features and Enhancements (available only on the java.sun.com website) Java Language Java Programming Language Virtual Machine Virtual Machine Base Libraries java.lang, java.util Packages Instrumentation Language and Utility Packages Monitoring and Management Package Version Identification Reference Objects Reflection Collections Framework Concurrency Utilities Java Archive (JAR) Files Logging Preferences Regular Expressions Zip Files Other packages Math I/O Object Serialization Networking Endorsed Standards Override Mechanism Extension Mechanism Internationalization JavaBeansTM Component API Java Management Extensions (JMX) Java Native Interface (JNI) Security XML (JAXP) Integration Libraries Java Database Connectivity (JDBCTM) Java IDL Java Naming and Directory InterfaceTM (JNDI) Remote Method Invocation (RMI) RMI-IIOP Scripting for the Java Platform User Interface Libraries 2D Graphics and Imaging Accessibility Abstract Window Toolkit (AWT) Drag-and-Drop Data Transfer Image I/O Input Method Framework Print Service Sound Swing Components Deployment General Deployment Java Plug-in Java Web Start Tools Annotation Processing Tool Attach API javac Tool Javadoc Tool Java Platform Debugger Architecture (JPDA) Java Debug Interface (JDI) Java Debug Wire Protocol (JDWP) JVMTM Tool Interface (JVM TI) (replaces JVMPI and JVMDI) -------------------------------------------------------------------------------- JDK Tools and Utilities Reference documentation for the JDK tools and utilities. JDK Tool and Utility Documentation Including Troubleshooting and Diagnostic Information -------------------------------------------------------------------------------- Tutorials, Training, Demos, Samples, and Other Information Learning about Java. The Java Tutorial An example-filled guide to the Java programming language and core APIs. Java Technology Home Page Training for the Java programming language Directory of various training resources. Demonstration Applets and Applications (included in the JDK documentation bundle and on java.sun.com ) On-Line Courses for the Java Programming Language Java Web Start Samples (included in the JDK documentation bundle and on java.sun.com ) Java Series Books Effective Java Best selling guide about best programming practices. Java Documentation in HTMLHelp and WinHelp Formats For Windows users. Code Conventions for the Java Programming Language Standards and styles for coding Java programs. New-to-JavaTM Programming Center. New to the Java Platform. Java™ Platform Standard Ed. 6 All Classes Packages java.applet java.awt java.awt.color java.awt.datatransfer java.awt.dnd java.awt.event java.awt.font java.awt.geom java.awt.im java.awt.im.spi java.awt.image java.awt.image.renderable java.awt.print java.beans java.beans.beancontext java.io java.lang java.lang.annotation java.lang.instrument java.lang.management java.lang.ref java.lang.reflect java.math java.net java.nio java.nio.channels java.nio.channels.spi java.nio.charset java.nio.charset.spi java.rmi java.rmi.activation java.rmi.dgc java.rmi.registry java.rmi.server java.security java.security.acl java.security.cert java.security.interfaces java.security.spec java.sql java.text java.text.spi java.util java.util.concurrent java.util.concurrent.atomic java.util.concurrent.locks java.util.jar java.util.logging java.util.prefs java.util.regex java.util.spi java.util.zip javax.accessibility javax.activation javax.activity javax.annotation javax.annotation.processing javax.crypto javax.crypto.interfaces javax.crypto.spec javax.imageio javax.imageio.event javax.imageio.metadata javax.imageio.plugins.bmp javax.imageio.plugins.jpeg javax.imageio.spi javax.imageio.stream javax.jws javax.jws.soap javax.lang.model javax.lang.model.element javax.lang.model.type javax.lang.model.util javax.management javax.management.loading javax.management.modelmbean javax.management.monitor javax.management.openmbean javax.management.relation javax.management.remote javax.management.remote.rmi javax.management.timer javax.naming javax.naming.directory javax.naming.event javax.naming.ldap javax.naming.spi javax.net javax.net.ssl javax.print javax.print.attribute javax.print.attribute.standard javax.print.event javax.rmi javax.rmi.CORBA javax.rmi.ssl javax.script javax.security.auth javax.security.auth.callback javax.security.auth.kerberos javax.security.auth.login javax.security.auth.spi javax.security.auth.x500 javax.security.cert javax.security.sasl javax.sound.midi javax.sound.midi.spi javax.sound.sampled javax.sound.sampled.spi javax.sql javax.sql.rowset javax.sql.rowset.serial javax.sql.rowset.spi javax.swing javax.swing.border javax.swing.colorchooser javax.swing.event javax.swing.filechooser javax.swing.plaf javax.swing.plaf.basic javax.swing.plaf.metal javax.swing.plaf.multi javax.swing.plaf.synth javax.swing.table javax.swing.text javax.swing.text.html javax.swing.text.html.parser javax.swing.text.rtf javax.swing.tree javax.swing.undo javax.tools javax.transaction javax.transaction.xa javax.xml javax.xml.bind javax.xml.bind.annotation javax.xml.bind.annotation.adapters javax.xml.bind.attachment javax.xml.bind.helpers javax.xml.bind.util javax.xml.crypto javax.xml.crypto.dom javax.xml.crypto.dsig javax.xml.crypto.dsig.dom javax.xml.crypto.dsig.keyinfo javax.xml.crypto.dsig.spec javax.xml.datatype javax.xml.namespace javax.xml.parsers javax.xml.soap javax.xml.stream javax.xml.stream.events javax.xml.stream.util javax.xml.transform javax.xml.transform.dom javax.xml.transform.sax javax.xml.transform.stax javax.xml.transform.stream javax.xml.validation javax.xml.ws javax.xml.ws.handler javax.xml.ws.handler.soap javax.xml.ws.http javax.xml.ws.soap javax.xml.ws.spi javax.xml.xpath org.ietf.jgss org.omg.CORBA org.omg.CORBA_2_3 org.omg.CORBA_2_3.portable org.omg.CORBA.DynAnyPackage org.omg.CORBA.ORBPackage org.omg.CORBA.portable org.omg.CORBA.TypeCodePackage org.omg.CosNaming org.omg.CosNaming.NamingContextExtPackage org.omg.CosNaming.NamingContextPackage org.omg.Dynamic org.omg.DynamicAny org.omg.DynamicAny.DynAnyFactoryPackage org.omg.DynamicAny.DynAnyPackage org.omg.IOP org.omg.IOP.CodecFactoryPackage org.omg.IOP.CodecPackage org.omg.Messaging org.omg.PortableInterceptor org.omg.PortableInterceptor.ORBInitInfoPackage org.omg.PortableServer org.omg.PortableServer.CurrentPackage org.omg.PortableServer.POAManagerPackage org.omg.PortableServer.POAPackage org.omg.PortableServer.portable org.omg.PortableServer.ServantLocatorPackage org.omg.SendingContext org.omg.stub.java.rmi org.w3c.dom org.w3c.dom.bootstrap org.w3c.dom.events org.w3c.dom.ls org.xml.sax org.xml.sax.ext org.xml.sax.helpers

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧