java jini dll,Java开发网 - 寻找学习jini的朋友

本文介绍了一个使用Java Jini技术实现的服务发布系统。该系统通过`HelloWorldServicea`类来发布服务,并利用`EventGena`接口实现远程事件监听与注册。文章详细展示了如何创建服务项、设置安全策略、发现查找服务以及处理服务注册过程。
摘要由CSDN通过智能技术生成

Posted by:h_q_x

Posted on:2003-03-25 21:28

package corejini.chapter5;

import net.jini.discovery.DiscoveryListener;

import net.jini.discovery.DiscoveryEvent;

import net.jini.discovery.LookupDiscovery;

import net.jini.core.lookup.ServiceItem;

import net.jini.core.lookup.ServiceRegistrar;

import net.jini.core.lookup.ServiceRegistration;

import net.jini.core.entry.Entry;

import net.jini.lookup.entry.*;

import net.jini.core.event.*;

import java.util.HashMap;

import java.io.IOException;

import java.io.Serializable;

import java.rmi.RemoteException;

import java.rmi.Remote;

import java.rmi.RMISecurityManager;

import java.rmi.MarshalledObject;

import java.rmi.server.UnicastRemoteObject;

import javax.swing.event.EventListenerList;

// This is the proxy object that will be downloaded

// by clients. It's serializable and implements

// our well-known HelloWorldServiceInterface.

add here for EVENT GENERATOR!!!

interface EventGena extends Remote{

public EventRegistration register(RemoteEventListener listener,

long leaseLength)

throws RemoteException;

}

// HelloWorldService is the "wrapper" class that

// handles publishing the service item.

public class HelloWorldServicea extends UnicastRemoteObject implements EventGena,Runnable {

// 10 minute leases

protected final int LEASE_TIME = 1000 * 60 * 1000;

protected HashMap registrations = new HashMap();

protected ServiceItem item;

protected LookupDiscovery disco;

//here r the variants for the EVENT GEN

protected long lastEventSeqNo = 0;

EventListenerList listenerList = new EventListenerList();

// Inner class to listen for discovery events

class Listener implements DiscoveryListener {

// Called when we find a new lookup service.

public void discovered(DiscoveryEvent ev) {

System.out.println("discovered a lookup service!");

ServiceRegistrar[] newregs = ev.getRegistrars();

for (int i=0 ; i

if (!registrations.containsKey(newregs[i])) {

registerWithLookup(newregs[i]);

}

}

}

// Called ONLY when we explicitly discard a

// lookup service, not "automatically" when a

// lookup service goes down. Once discovered,

// there is NO ongoing communication with a

// lookup service.

public void discarded(DiscoveryEvent ev) {

ServiceRegistrar[] deadregs = ev.getRegistrars();

for (int i=0 ; i

registrations.remove(deadregs[i]);

}

}

}

public HelloWorldServicea() throws IOException {

item = new ServiceItem(null, this

, null);

// Set a security manager

if (System.getSecurityManager() == null) {

System.setSecurityManager(new RMISecurityManager());

}

// Search for the "public" group, which by

// convention is named by the empty string

disco = new LookupDiscovery(new String[] { "" });

// Install a listener.

disco.addDiscoveryListener(new Listener());

}

// This work involves remote calls, and may take a

// while to complete. Thus, since it's called from

// discovered(), it will prevent us from responding

// in a timely fashion to new discovery events. An

// improvement would be to spin off a separate short-

// lived thread to do the work.

synchronized void registerWithLookup(ServiceRegistrar

registrar) {

ServiceRegistration registration = null;

System.out.println("enter lookup register...");

try {

registration = registrar.register(item, LEASE_TIME);

} catch (RemoteException ex) {

System.out.println("Couldn't register: " + ex.getMessage());

return;

} catch (Exception ex){

System.out.println("Couldn't register: " + ex.getMessage());

return;

}

// If this is our first registration, use the

// service ID returned to us. Ideally, we should

// save this ID so that it can be used after

// restarts of the service

if (item.serviceID == null) {

item.serviceID = registration.getServiceID();

System.out.println("Set serviceID to " + item.serviceID);

}

System.out.println("serviceID is set to " + item.serviceID);

registrations.put(registrar, registration);

}

//here is the method for EVENT GEN

public synchronized EventRegistration register(

RemoteEventListener listener,

long leaseLength){

EventRegistration evtreg = new EventRegistration(1L,this,null,0);

listenerList.add(RemoteEventListener.class, listener);

return evtreg;

//

}

//here is the one to send those EVENT

protected void sendEvent(){

RemoteEvent remoteEvent = null;

// Guaranteed to return a non-null array

Object[] listeners = listenerList.getListenerList();

// Process the listeners last to first, notifying

// those that are interested in this event

for (int n = listeners.length - 2; n >= 0; n -= 2) {

if (listeners[n] == RemoteEventListener.class) {

RemoteEventListener listener =

(RemoteEventListener) listeners[n+1];

if (remoteEvent == null) {

remoteEvent = new RemoteEvent(this, 1L,

lastEventSeqNo++, null);

}

try {

listener.notify(remoteEvent);

} catch(UnknownEventException e) {

e.printStackTrace();

} catch(java.rmi.RemoteException e) {

e.printStackTrace();

}

}

}

}

// This thread does nothing but sleep, but it

// makes sure the VM doesn't exit.

public void run() {

while (true) {

try {

Thread.sleep(1000000);

sendEvent();

} catch (InterruptedException ex) {

}

}

}

// Create a new HelloWorldService and start

// its thread.

public static void main(String args[]) {

try {

HelloWorldServicea hws =

new HelloWorldServicea();

new Thread(hws).start();

} catch (IOException ex) {

System.out.println("Couldn't create service: " +

ex.getMessage());

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值