SlideShare une entreprise Scribd logo
1  sur  44
Emrah KAHRAMAN
Software Development Specialist
Remote Method Invocation
Uzak Metod Çağırma
Uzak nesne haberleşmesinde kullanılan RMI
Java nın dağıtık nesneleri için destek sağlar.
RMI uzak nesneler üzerindeki metodların
çağrılmasına izin verir.
RMI dağıtık nesneler kullanılarak dağıtık
uygulamalar gerçekleştirmek için kullanılan
yüksek seviyeli Java API’sidir.
Uzak nesne çağrımlarında aynı Java söz
dizimini kullanabilirsiniz.
Java RMI modeli iki genel gereksinime sahiptir.
Birinci gereksinim RMI model basit ve
kullanımı kolay olmalı.
İkinci gereksinim ise Java dili doğal bir
şekilde kullanılmalıdır.
RMI’ın anahtar fikri nesneler arasında
haberleşmenin kolaylaşmasında stub ve skeletonlar
kullanır.
Stub ve skeleton otomatik üretilirken
programcıların socket seviyeli ağ programlamadaki
can sıkıntısını hafifletir.
RMI,
parametrelerin ağ üzerinde belirlenen formatta
gönderilmesi için
kodlanmasında (parameter marshalling)
ve bunların
geri elde edilmesinde (parameter unmarshalling)
nesne serileştirmeyi (object serialization)
kullanmaktadır.
İstemcilerin uzaktaki nesnelerin metotlarını RMI ile çağrılabilmeleri için,
söz konusu uzak nesnelere ilişkin referansları elde etmesi gerekmektedir.
RMI modelinde bu amaca yönelik olarak bir isim sunucu (name server)
kullanılmaktadır.
Uzak nesneler java.rmi.Naming isimli bir sınıfta bulunan metodları
kullanarak kendilerini İsim Kayıtçısı (Naming Registry) servisine
kaydettirmelidirler.
1-) Remote Interface’den extends edilen
Interface
2-) Server Sınıfı
3-) Oluşturduğumuz Interface’i
implement eden sınıf
4-) Client (İstek yapacağımız) Sınıfı
1-) java.rmi.Remote interfacesinden extends
edilmiş olmalı
2-) RMI tarafından gerçekleştirilecek metodlar
deklare edilmeli
3-) Her metod RemoteException fırlatmalı
Registry reg= LocateRegistry.getRegistry();
reg.rebind("StudentServerInterfaceImpl", new StudentServerInterfaceImpl();
Aşağıdaki gibi önce Registry’i referans alan bir
nesne tanımlanmalı
LocateRegistry içindeki getRegistry() metodu
Parametre almayabilir
Sadece Serverın çalışacağı portu parametre alabilir
Sadece Serverın çalışacağı makinenin adresini
parametre alabilir
Serverın çalışacağı makinenin port numarasını ve
adresini (host) parametre alabilir
Registry içindeki metodlar
Bind() metodu ile uzak nesne ye (remote object)bir isim verilerek Registrye kayıt
edilir.
Rebind() metodu ile uzak nesneye isim verilir daha önceden uzak nesneye isim
verilmiş ise eski isim yeni isim ile değiştirilir.
Unbind() metodu uzak nesne lere verilen isimleri imha eder.
List() metodu registry de bulunan tüm nesnelerin isimlerini dizi şeklinde geri
döndürür.
Lookup() ile daha önceden bind() edilmiş uzak nesnelere isim üzerinden erişilir.
UnicastRemoteObject’ten extends edilmelidir.
Daha önce oluşturduğumuz interfaceden
implement edilmelidir.
Önceden oluşturduğumuz interfacedeki
metodların içleri doldurulmalıdır
(implementation).
Metodlar RemoteException fırlatmalıdır.
HesapMakinesi h=null;
h=(HesapMakinesi)Naming.lookup("rmi://127.0.0.1:1099/HesapMakinesiServisi");
h.cikar(9, 3) ;
Oluşturduğumuz Interfaceden referans elde
ederek lookup metodu ile servera host , port bilgilerini
ve uzak nesneye verdiğimiz isim ile bağlanırız.Sonra
elimizdeki uzak nesne ile server üzerindeki metodlara
erişebiliriz.
Eğer client ve server farklı makinelerde ise her iki
makinedede oluşturduğumuz interface olması gerekir.
RMI da server, client ve registry 3 farklı makinede
olabilir.
import java.rmi.*;
public interface HesapMakinesi extends Remote {
//Tüm metodlarımızı tanımladık
public long topla(long a, long b) throws RemoteException;
public long cikar(long a, long b) throws RemoteException;
public long carp(long a, long b) throws RemoteException;
public long bol(long a, long b) throws RemoteException;
}
Hesap Makinesi İnterface
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class HesapMakinesiGerceklestir
extends UnicastRemoteObject implements HesapMakinesi {
//HesapMakinesi Interfacesindeki metodlarımızın içlerini bu sınıfımda dolduruz.
private static final long serialVersionUID = 1L;
public HesapMakinesiGerceklestir() throws RemoteException {
super();
}
public long topla(long a, long b)throws RemoteException {
return a + b;
}
public long cikar(long a, long b) throws RemoteException {
return a - b;
}
public long carp(long a, long b) throws RemoteException {
return a * b;
}
public long bol(long a, long b) throws RemoteException {
return a / b;
}}
Interfacedeki Metodları Gerçekleştiren Sınıf
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject; /* Client'ın kullanabileceği Remote objeler oluşturmak
için kullanılır */
public class HesapMakineSunucu {
public HesapMakineSunucu() {
try {
Registry reg=LocateRegistry.createRegistry(1099); // Burada 1099 nolu portu
RMI Registrye kayıt ettik.
reg=LocateRegistry.getRegistry(); //Burada 1099 nolu portu çalıştırıyoruz.
reg.rebind("HesapMakinesiServisi", new HesapMakinesiGerceklestir());
//İstemciden gelen servis isteğine göre
}
catch (Exception e) {
System.out.println("Hata: " + e);
}
}
public static void main(String[] args) {
new HesapMakineSunucu();} //Sunucu nesnesi oluşturuldu
}
Hesap Makinesi Sunucusu
import java.rmi.Naming;
public class HesapMakinesiİstemci {
public static void main(String[] args) {
try {
/* Burada sunucun IP adresini girdik sonra sunucunun açık olan 1099 nolu portunu yazdık
ve bir servis ismi tanımadık. */
HesapMakinesi h=(HesapMakinesi)Naming.lookup("rmi://127.0.0.1:1099/HesapMakinesiServisi");
/*İşlemlerin metoduna değer yolladık gelecek cevabı konsolda yazdıracağız */
System.out.println( "Çıkarma Sonucu: "+h.cikar(9, 3) );
System.out.println( "Toplama Sonucu: "+h.topla(7, 5) );
System.out.println( "Çarpma Sonucu: " +h.carp(5, 6) );
System.out.println( "Bölme Sonucu: " +h.bol(9, 3) );
}
catch (Exception e) {
System.out.println(e.getMessage());
}
} }
Hesap Makinesi İstemcisi
import java.rmi.*;
public interface HesapMakinesi extends Remote {
//Tüm metodlarımızı tanımladık
public long topla(long a, long b) throws RemoteException;
public long cikar(long a, long b) throws RemoteException;
public long carp(long a, long b) throws RemoteException;
public long bol(long a, long b) throws RemoteException;
}
Hesap Makinesi İnterface
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class HesapMakinesiGerceklestir
extends UnicastRemoteObject implements HesapMakinesi {
//HesapMakinesi Interfacesindeki metodlarımızın içlerini bu sınıfımda dolduruz.
private static final long serialVersionUID = 1L;
public HesapMakinesiGerceklestir() throws RemoteException {
super();
}
public long topla(long a, long b)throws RemoteException {
return a + b;
}
public long cikar(long a, long b) throws RemoteException {
return a - b;
}
public long carp(long a, long b) throws RemoteException {
return a * b;
}
public long bol(long a, long b) throws RemoteException {
return a / b;
}}
HesapMakinesi Interfacedeki Metodları Gerçekleştiren SInıf
import java.rmi.*;
public interface Calculator extends Remote {
public long square(long a) throws RemoteException;
public long power(long a, long b) throws RemoteException;
public long mod(long a, long b) throws RemoteException;
}
Calculator İnterfacesi
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class CalculatorImpl
extends UnicastRemoteObject implements Calculator {
public CalculatorImpl() throws RemoteException {
super();
}
@Override
public long mod(long a, long b) throws RemoteException {
long c=a%b;
return c;
}
@Override
public long power(long a, long b) throws RemoteException {
return (long) Math.pow(a, b);
}
@Override
public long square(long a) throws RemoteException {
return (long) Math.sqrt(a);
}}
Calculator Interfacesindeki Metodlarını Gerçekleştiren Sınıf
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject; /* Client'ın kullanabileceği Remote objectler oluşturmak
için kullanılır */
public class HesapMakineSunucu {
public HesapMakineSunucu() {
try {
// Burada 1099 nolu portu RMI Registrye kayıt ettik.
Registry reg=LocateRegistry.createRegistry(1099);
reg=LocateRegistry.getRegistry(); //Burada 1099 nolu portu çalıştırıyoruz.
reg.rebind("HesapMakinesiServisi", new HesapMakinesiGerceklestir());
reg.rebind("BilimselHesapServisi", new CalculatorImpl());
//İstemciden gelen servis isteğine göre
}
catch (Exception e) {
System.out.println("Hata: " + e);
}
}
public static void main(String[] args) {
new HesapMakineSunucu();} //Sunucu nesnesi oluşturuldu
}
Hesap Makinesi Sunucusu
import java.rmi.Naming;
public class HesapMakinesiİstemci {
public static void main(String[] args) {
try {
/* Burada sunucun IP adresini girdik sonra sunucunun açık olan 1099 nolu portunu yazdık
ve bir servis ismi tanımadık. */
HesapMakinesi h=(HesapMakinesi)Naming.lookup("rmi://127.0.0.1:1099/HesapMakinesiServisi");
/*İşlemlerin metoduna değer yolladık gelecek cevabı konsolda yazdıracağız */
System.out.println( "Çıkarma Sonucu: "+h.cikar(9, 3) );
System.out.println( "Toplama Sonucu: "+h.topla(7, 5) );
System.out.println( "Çarpma Sonucu: " +h.carp(5, 6) );
System.out.println( "Bölme Sonucu: " +h.bol(9, 3) );
System.out.println( "Mod Alma Sonucu: "+c.mod(7, 5) );
System.out.println( "Üs Alma Sonucu: " +c.power(5, 6) );
System.out.println( "Karekök Sonucu: " +c.square(9) );
}
catch (Exception e) {
System.out.println(e.getMessage());
}
} }
Hesap Makinesi İstemcisi
Konsol Görüntüsü
Çıkarma Sonucu: 6
Toplama Sonucu: 12
Çarpma Sonucu: 30
Bölme Sonucu: 3
Mod Alma Sonucu: 2
Üs Alma Sonucu: 15625
Karekök Sonucu: 3
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Vector;
public interface Msn extends Remote
{
public void mesajKaydet(String ad, String str) throws RemoteException;
public Vector mesajAl() throws RemoteException;
}
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;
public class MsnImpl extends UnicastRemoteObject implements Msn {
private static final long serialVersionUID = 1L;
Vector x=new Vector();
protected MsnImpl() throws RemoteException {
super();
}
public void mesajKaydet(String ad, String msg)
{
x.addElement(ad + " : "+msg);
}
public Vector mesajAl()
{
return x;
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class MsnServer {
public MsnServer() {
try {
Registry reg=LocateRegistry.createRegistry(1099);
reg=LocateRegistry.getRegistry();
reg.rebind("MsnService", new MsnImpl());
}
catch (Exception e) {
System.out.println("Trouble: " + e);
}
}
public static void main(String[] args) {
new MsnServer();
}
}
import javax.swing.SwingUtilities;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Rectangle;
import java.rmi.Naming;
import java.rmi.RemoteException;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JList;
public class MsnClient extends JFrame implements
Runnable {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JTextField txt01 = null;
private JLabel lbl01 = null;
private JLabel lbl02 = null;
private JButton btn = null;
private JList list = null;
private Msn msn;
private JTextField getTxt01()
{
if (txt01 == null)
{
txt01 = new JTextField();
txt01.setBounds(new Rectangle(205, 253, 353, 91));
txt01.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String ad="EMRAH";
String str=txt01.getText();
try {
msn.mesajKaydet(ad,str);
} catch (RemoteException re) {
System.err.println("RMI mesaj kaydetme hatasi: " + re.getMessage());
}
txt01.setText("");
}
});
}
return txt01;
}
private JButton getBtn() {
if (btn == null) {
btn = new JButton();
btn.setBounds(new Rectangle(565, 254, 82, 89));
btn.setText("Gönder");
btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String ad="EMRAH";
String str=txt01.getText();
try {
msn.mesajKaydet(ad,str);
} catch (RemoteException re) {
System.err.println("RMI mesaj kaydetme hatasi: " + re.getMessage());
}
txt01.setText("");
}
});
}
return btn;
}
private JList getList() {
if (list == null) {
list = new JList();
list.setBounds(new Rectangle(204, 12, 441, 215));
}
return list;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MsnClient thisClass = new MsnClient();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public MsnClient() {
super();
initialize();
try {
msn = (Msn) Naming.lookup("rmi://localhost:1099/MsnService");
} catch (Exception e) {
System.out.println("MSN RMI hatasi: " + e.getMessage());
}
// yeni bir thread oluştur
Thread t = new Thread(this);
t.start();
}
@Override
public void run()
{
while(true) {
try {
list.setListData(msn.mesajAl());
} catch (RemoteException e) {
System.err.println(e.getMessage());
}
}
}
private void initialize() {
this.setSize(684, 400);
this.setContentPane(getJContentPane());
this.setTitle(“Chat Uygulaması");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
lbl02 = new JLabel();
lbl02.setBounds(new Rectangle(18, 227, 150, 113));
lbl02.setText(" EMRAH");
lbl01 = new JLabel();
lbl01.setBounds(new Rectangle(17, 30, 142, 104));
lbl01.setText(" VOLKAN");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getTxt01(), null);
jContentPane.add(lbl01, null);
jContentPane.add(lbl02, null);
jContentPane.add(getBtn(), null);
jContentPane.add(getList(), null);
}
return jContentPane;
}
}
Kaynaklar
http://tr.wikipedia.org/wiki/CORBA
Java Remote Method Invocation - Geylani KARDAŞ
Introduction to Java Programming 6. Edition
Y.Daniel Liang

Contenu connexe

Similaire à Emrah KAHRAMAN - Java RMI

Java scriptxmlhttprequest
Java scriptxmlhttprequestJava scriptxmlhttprequest
Java scriptxmlhttprequest
MZeki Osmancık
 

Similaire à Emrah KAHRAMAN - Java RMI (17)

Spring uygulamaların exception handling yönetimi
Spring uygulamaların exception handling yönetimiSpring uygulamaların exception handling yönetimi
Spring uygulamaların exception handling yönetimi
 
3. aşırı yükleme yaşam alanı - dinamik bellek kullanımı
3. aşırı yükleme   yaşam alanı - dinamik bellek kullanımı3. aşırı yükleme   yaşam alanı - dinamik bellek kullanımı
3. aşırı yükleme yaşam alanı - dinamik bellek kullanımı
 
Android Uygulamaların Tersine Mühendislik Yöntemi ile İncelenmesi
Android Uygulamaların Tersine Mühendislik Yöntemi ile İncelenmesiAndroid Uygulamaların Tersine Mühendislik Yöntemi ile İncelenmesi
Android Uygulamaların Tersine Mühendislik Yöntemi ile İncelenmesi
 
Java scriptxmlhttprequest
Java scriptxmlhttprequestJava scriptxmlhttprequest
Java scriptxmlhttprequest
 
Recep proje 5
Recep proje 5Recep proje 5
Recep proje 5
 
Mutant Web Applications
Mutant Web ApplicationsMutant Web Applications
Mutant Web Applications
 
C#, Microsoft Yaz Okulu 2011 - İzmir
C#, Microsoft Yaz Okulu 2011 - İzmirC#, Microsoft Yaz Okulu 2011 - İzmir
C#, Microsoft Yaz Okulu 2011 - İzmir
 
Girişimciler ve Geliştiriciler için Android - Etkin Android Programlama
Girişimciler ve Geliştiriciler için Android - Etkin Android ProgramlamaGirişimciler ve Geliştiriciler için Android - Etkin Android Programlama
Girişimciler ve Geliştiriciler için Android - Etkin Android Programlama
 
Php1
Php1Php1
Php1
 
Javascript Performance Optimisation
Javascript Performance OptimisationJavascript Performance Optimisation
Javascript Performance Optimisation
 
9. şablonlar
9. şablonlar9. şablonlar
9. şablonlar
 
Asp.net mvc ve jquery ile sunucudan json verisi okuma
Asp.net mvc ve jquery ile sunucudan json verisi okumaAsp.net mvc ve jquery ile sunucudan json verisi okuma
Asp.net mvc ve jquery ile sunucudan json verisi okuma
 
Java EE Struts
Java EE StrutsJava EE Struts
Java EE Struts
 
Nginx ve Unicorn'la Rack Uygulamalarını Koşturmak
Nginx ve Unicorn'la Rack Uygulamalarını KoşturmakNginx ve Unicorn'la Rack Uygulamalarını Koşturmak
Nginx ve Unicorn'la Rack Uygulamalarını Koşturmak
 
Spring Framework
Spring FrameworkSpring Framework
Spring Framework
 
.Net ile yazılım güvenliği
.Net ile yazılım güvenliği.Net ile yazılım güvenliği
.Net ile yazılım güvenliği
 
Android'e Giriş Eğitimleri 1_2
Android'e Giriş Eğitimleri 1_2Android'e Giriş Eğitimleri 1_2
Android'e Giriş Eğitimleri 1_2
 

Emrah KAHRAMAN - Java RMI

  • 2. Remote Method Invocation Uzak Metod Çağırma
  • 3. Uzak nesne haberleşmesinde kullanılan RMI Java nın dağıtık nesneleri için destek sağlar. RMI uzak nesneler üzerindeki metodların çağrılmasına izin verir.
  • 4. RMI dağıtık nesneler kullanılarak dağıtık uygulamalar gerçekleştirmek için kullanılan yüksek seviyeli Java API’sidir. Uzak nesne çağrımlarında aynı Java söz dizimini kullanabilirsiniz.
  • 5. Java RMI modeli iki genel gereksinime sahiptir. Birinci gereksinim RMI model basit ve kullanımı kolay olmalı. İkinci gereksinim ise Java dili doğal bir şekilde kullanılmalıdır.
  • 6. RMI’ın anahtar fikri nesneler arasında haberleşmenin kolaylaşmasında stub ve skeletonlar kullanır. Stub ve skeleton otomatik üretilirken programcıların socket seviyeli ağ programlamadaki can sıkıntısını hafifletir.
  • 7. RMI, parametrelerin ağ üzerinde belirlenen formatta gönderilmesi için kodlanmasında (parameter marshalling) ve bunların geri elde edilmesinde (parameter unmarshalling) nesne serileştirmeyi (object serialization) kullanmaktadır.
  • 8. İstemcilerin uzaktaki nesnelerin metotlarını RMI ile çağrılabilmeleri için, söz konusu uzak nesnelere ilişkin referansları elde etmesi gerekmektedir. RMI modelinde bu amaca yönelik olarak bir isim sunucu (name server) kullanılmaktadır. Uzak nesneler java.rmi.Naming isimli bir sınıfta bulunan metodları kullanarak kendilerini İsim Kayıtçısı (Naming Registry) servisine kaydettirmelidirler.
  • 9. 1-) Remote Interface’den extends edilen Interface 2-) Server Sınıfı 3-) Oluşturduğumuz Interface’i implement eden sınıf 4-) Client (İstek yapacağımız) Sınıfı
  • 10. 1-) java.rmi.Remote interfacesinden extends edilmiş olmalı 2-) RMI tarafından gerçekleştirilecek metodlar deklare edilmeli 3-) Her metod RemoteException fırlatmalı
  • 11. Registry reg= LocateRegistry.getRegistry(); reg.rebind("StudentServerInterfaceImpl", new StudentServerInterfaceImpl(); Aşağıdaki gibi önce Registry’i referans alan bir nesne tanımlanmalı
  • 12. LocateRegistry içindeki getRegistry() metodu Parametre almayabilir Sadece Serverın çalışacağı portu parametre alabilir Sadece Serverın çalışacağı makinenin adresini parametre alabilir Serverın çalışacağı makinenin port numarasını ve adresini (host) parametre alabilir
  • 13. Registry içindeki metodlar Bind() metodu ile uzak nesne ye (remote object)bir isim verilerek Registrye kayıt edilir. Rebind() metodu ile uzak nesneye isim verilir daha önceden uzak nesneye isim verilmiş ise eski isim yeni isim ile değiştirilir. Unbind() metodu uzak nesne lere verilen isimleri imha eder. List() metodu registry de bulunan tüm nesnelerin isimlerini dizi şeklinde geri döndürür. Lookup() ile daha önceden bind() edilmiş uzak nesnelere isim üzerinden erişilir.
  • 14. UnicastRemoteObject’ten extends edilmelidir. Daha önce oluşturduğumuz interfaceden implement edilmelidir. Önceden oluşturduğumuz interfacedeki metodların içleri doldurulmalıdır (implementation). Metodlar RemoteException fırlatmalıdır.
  • 15. HesapMakinesi h=null; h=(HesapMakinesi)Naming.lookup("rmi://127.0.0.1:1099/HesapMakinesiServisi"); h.cikar(9, 3) ; Oluşturduğumuz Interfaceden referans elde ederek lookup metodu ile servera host , port bilgilerini ve uzak nesneye verdiğimiz isim ile bağlanırız.Sonra elimizdeki uzak nesne ile server üzerindeki metodlara erişebiliriz.
  • 16. Eğer client ve server farklı makinelerde ise her iki makinedede oluşturduğumuz interface olması gerekir. RMI da server, client ve registry 3 farklı makinede olabilir.
  • 17.
  • 18. import java.rmi.*; public interface HesapMakinesi extends Remote { //Tüm metodlarımızı tanımladık public long topla(long a, long b) throws RemoteException; public long cikar(long a, long b) throws RemoteException; public long carp(long a, long b) throws RemoteException; public long bol(long a, long b) throws RemoteException; } Hesap Makinesi İnterface
  • 19. import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class HesapMakinesiGerceklestir extends UnicastRemoteObject implements HesapMakinesi { //HesapMakinesi Interfacesindeki metodlarımızın içlerini bu sınıfımda dolduruz. private static final long serialVersionUID = 1L; public HesapMakinesiGerceklestir() throws RemoteException { super(); } public long topla(long a, long b)throws RemoteException { return a + b; } public long cikar(long a, long b) throws RemoteException { return a - b; } public long carp(long a, long b) throws RemoteException { return a * b; } public long bol(long a, long b) throws RemoteException { return a / b; }} Interfacedeki Metodları Gerçekleştiren Sınıf
  • 20. import java.rmi.Naming; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; /* Client'ın kullanabileceği Remote objeler oluşturmak için kullanılır */ public class HesapMakineSunucu { public HesapMakineSunucu() { try { Registry reg=LocateRegistry.createRegistry(1099); // Burada 1099 nolu portu RMI Registrye kayıt ettik. reg=LocateRegistry.getRegistry(); //Burada 1099 nolu portu çalıştırıyoruz. reg.rebind("HesapMakinesiServisi", new HesapMakinesiGerceklestir()); //İstemciden gelen servis isteğine göre } catch (Exception e) { System.out.println("Hata: " + e); } } public static void main(String[] args) { new HesapMakineSunucu();} //Sunucu nesnesi oluşturuldu } Hesap Makinesi Sunucusu
  • 21. import java.rmi.Naming; public class HesapMakinesiİstemci { public static void main(String[] args) { try { /* Burada sunucun IP adresini girdik sonra sunucunun açık olan 1099 nolu portunu yazdık ve bir servis ismi tanımadık. */ HesapMakinesi h=(HesapMakinesi)Naming.lookup("rmi://127.0.0.1:1099/HesapMakinesiServisi"); /*İşlemlerin metoduna değer yolladık gelecek cevabı konsolda yazdıracağız */ System.out.println( "Çıkarma Sonucu: "+h.cikar(9, 3) ); System.out.println( "Toplama Sonucu: "+h.topla(7, 5) ); System.out.println( "Çarpma Sonucu: " +h.carp(5, 6) ); System.out.println( "Bölme Sonucu: " +h.bol(9, 3) ); } catch (Exception e) { System.out.println(e.getMessage()); } } } Hesap Makinesi İstemcisi
  • 22.
  • 23. import java.rmi.*; public interface HesapMakinesi extends Remote { //Tüm metodlarımızı tanımladık public long topla(long a, long b) throws RemoteException; public long cikar(long a, long b) throws RemoteException; public long carp(long a, long b) throws RemoteException; public long bol(long a, long b) throws RemoteException; } Hesap Makinesi İnterface
  • 24. import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class HesapMakinesiGerceklestir extends UnicastRemoteObject implements HesapMakinesi { //HesapMakinesi Interfacesindeki metodlarımızın içlerini bu sınıfımda dolduruz. private static final long serialVersionUID = 1L; public HesapMakinesiGerceklestir() throws RemoteException { super(); } public long topla(long a, long b)throws RemoteException { return a + b; } public long cikar(long a, long b) throws RemoteException { return a - b; } public long carp(long a, long b) throws RemoteException { return a * b; } public long bol(long a, long b) throws RemoteException { return a / b; }} HesapMakinesi Interfacedeki Metodları Gerçekleştiren SInıf
  • 25. import java.rmi.*; public interface Calculator extends Remote { public long square(long a) throws RemoteException; public long power(long a, long b) throws RemoteException; public long mod(long a, long b) throws RemoteException; } Calculator İnterfacesi
  • 26. import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class CalculatorImpl extends UnicastRemoteObject implements Calculator { public CalculatorImpl() throws RemoteException { super(); } @Override public long mod(long a, long b) throws RemoteException { long c=a%b; return c; } @Override public long power(long a, long b) throws RemoteException { return (long) Math.pow(a, b); } @Override public long square(long a) throws RemoteException { return (long) Math.sqrt(a); }} Calculator Interfacesindeki Metodlarını Gerçekleştiren Sınıf
  • 27. import java.rmi.Naming; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; /* Client'ın kullanabileceği Remote objectler oluşturmak için kullanılır */ public class HesapMakineSunucu { public HesapMakineSunucu() { try { // Burada 1099 nolu portu RMI Registrye kayıt ettik. Registry reg=LocateRegistry.createRegistry(1099); reg=LocateRegistry.getRegistry(); //Burada 1099 nolu portu çalıştırıyoruz. reg.rebind("HesapMakinesiServisi", new HesapMakinesiGerceklestir()); reg.rebind("BilimselHesapServisi", new CalculatorImpl()); //İstemciden gelen servis isteğine göre } catch (Exception e) { System.out.println("Hata: " + e); } } public static void main(String[] args) { new HesapMakineSunucu();} //Sunucu nesnesi oluşturuldu } Hesap Makinesi Sunucusu
  • 28. import java.rmi.Naming; public class HesapMakinesiİstemci { public static void main(String[] args) { try { /* Burada sunucun IP adresini girdik sonra sunucunun açık olan 1099 nolu portunu yazdık ve bir servis ismi tanımadık. */ HesapMakinesi h=(HesapMakinesi)Naming.lookup("rmi://127.0.0.1:1099/HesapMakinesiServisi"); /*İşlemlerin metoduna değer yolladık gelecek cevabı konsolda yazdıracağız */ System.out.println( "Çıkarma Sonucu: "+h.cikar(9, 3) ); System.out.println( "Toplama Sonucu: "+h.topla(7, 5) ); System.out.println( "Çarpma Sonucu: " +h.carp(5, 6) ); System.out.println( "Bölme Sonucu: " +h.bol(9, 3) ); System.out.println( "Mod Alma Sonucu: "+c.mod(7, 5) ); System.out.println( "Üs Alma Sonucu: " +c.power(5, 6) ); System.out.println( "Karekök Sonucu: " +c.square(9) ); } catch (Exception e) { System.out.println(e.getMessage()); } } } Hesap Makinesi İstemcisi
  • 29. Konsol Görüntüsü Çıkarma Sonucu: 6 Toplama Sonucu: 12 Çarpma Sonucu: 30 Bölme Sonucu: 3 Mod Alma Sonucu: 2 Üs Alma Sonucu: 15625 Karekök Sonucu: 3
  • 30.
  • 31. import java.rmi.Remote; import java.rmi.RemoteException; import java.util.Vector; public interface Msn extends Remote { public void mesajKaydet(String ad, String str) throws RemoteException; public Vector mesajAl() throws RemoteException; }
  • 32. import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import java.util.Vector; public class MsnImpl extends UnicastRemoteObject implements Msn { private static final long serialVersionUID = 1L; Vector x=new Vector(); protected MsnImpl() throws RemoteException { super(); }
  • 33. public void mesajKaydet(String ad, String msg) { x.addElement(ad + " : "+msg); } public Vector mesajAl() { return x; } }
  • 34. import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; public class MsnServer { public MsnServer() { try { Registry reg=LocateRegistry.createRegistry(1099); reg=LocateRegistry.getRegistry(); reg.rebind("MsnService", new MsnImpl()); } catch (Exception e) { System.out.println("Trouble: " + e); } }
  • 35. public static void main(String[] args) { new MsnServer(); } }
  • 36. import javax.swing.SwingUtilities; import javax.swing.JPanel; import javax.swing.JFrame; import java.awt.Rectangle; import java.rmi.Naming; import java.rmi.RemoteException; import javax.swing.JTextField; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JList;
  • 37. public class MsnClient extends JFrame implements Runnable { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JTextField txt01 = null; private JLabel lbl01 = null; private JLabel lbl02 = null; private JButton btn = null; private JList list = null; private Msn msn;
  • 38. private JTextField getTxt01() { if (txt01 == null) { txt01 = new JTextField(); txt01.setBounds(new Rectangle(205, 253, 353, 91)); txt01.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { String ad="EMRAH"; String str=txt01.getText(); try { msn.mesajKaydet(ad,str); } catch (RemoteException re) { System.err.println("RMI mesaj kaydetme hatasi: " + re.getMessage()); } txt01.setText(""); } }); } return txt01; }
  • 39. private JButton getBtn() { if (btn == null) { btn = new JButton(); btn.setBounds(new Rectangle(565, 254, 82, 89)); btn.setText("Gönder"); btn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { String ad="EMRAH"; String str=txt01.getText(); try { msn.mesajKaydet(ad,str); } catch (RemoteException re) { System.err.println("RMI mesaj kaydetme hatasi: " + re.getMessage()); } txt01.setText(""); } }); } return btn; }
  • 40. private JList getList() { if (list == null) { list = new JList(); list.setBounds(new Rectangle(204, 12, 441, 215)); } return list; } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { MsnClient thisClass = new MsnClient(); thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); thisClass.setVisible(true); } }); }
  • 41. public MsnClient() { super(); initialize(); try { msn = (Msn) Naming.lookup("rmi://localhost:1099/MsnService"); } catch (Exception e) { System.out.println("MSN RMI hatasi: " + e.getMessage()); } // yeni bir thread oluştur Thread t = new Thread(this); t.start(); }
  • 42. @Override public void run() { while(true) { try { list.setListData(msn.mesajAl()); } catch (RemoteException e) { System.err.println(e.getMessage()); } } } private void initialize() { this.setSize(684, 400); this.setContentPane(getJContentPane()); this.setTitle(“Chat Uygulaması"); }
  • 43. private JPanel getJContentPane() { if (jContentPane == null) { lbl02 = new JLabel(); lbl02.setBounds(new Rectangle(18, 227, 150, 113)); lbl02.setText(" EMRAH"); lbl01 = new JLabel(); lbl01.setBounds(new Rectangle(17, 30, 142, 104)); lbl01.setText(" VOLKAN"); jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(getTxt01(), null); jContentPane.add(lbl01, null); jContentPane.add(lbl02, null); jContentPane.add(getBtn(), null); jContentPane.add(getList(), null); } return jContentPane; } }
  • 44. Kaynaklar http://tr.wikipedia.org/wiki/CORBA Java Remote Method Invocation - Geylani KARDAŞ Introduction to Java Programming 6. Edition Y.Daniel Liang