Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1283130
  • 博文数量: 135
  • 博客积分: 10588
  • 博客等级: 上将
  • 技术积分: 1325
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-18 11:12
文章分类

全部博文(135)

文章存档

2013年(6)

2012年(3)

2011年(11)

2010年(7)

2009年(14)

2008年(6)

2007年(42)

2006年(46)

分类: Java

2006-09-25 15:08:37

现在看看servlet能不能调用这个,在netbeans中建立一个web工程,添加jade库,建立一个servletservlet中进行调用Agent,看能不能输出结果。
其中主要文件如下
文件名:StartServlet.java:
package examples;
/*
* StartServlet.java
 *
 * Created on 2006925, 上午10:55
 */
 
import java.io.*;
import java.net.*;
 
import javax.servlet.*;
import javax.servlet.http.*;
import jade.core.Runtime;
import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.behaviours.SimpleBehaviour;
import jade.wrapper.*;
import jade.core.Agent;
 
/**
 *
 * @author Administrator
 * @version
 */
public class StartServlet extends HttpServlet {
   
    /** Processes requests for both HTTP GET and POST methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
       
        // TODO output your page here
        out.println("");
        out.println("");
        out.println("Servlet StartServlet");
        out.println("");
        out.println("");
        out.println("

Servlet StartServlet at " + request.getContextPath () + "

");
        out.println("你好");
        try{
            Runtime rt = Runtime.instance();
            rt.setCloseVM(true);
            Profile pMain = new ProfileImpl(null, 8888, null);
           
            AgentContainer mc = rt.createMainContainer(pMain);
            // set now the default Profile to start a container
            ProfileImpl pContainer = new ProfileImpl(null, 8888, null);
            out.println("运行Agent容器 ..."+pContainer);
            AgentController rma = mc.createNewAgent("rma", "jade.tools.rma.rma", null);
            rma.start();
            AgentController custom=mc.createNewAgent("custom","examples.CustomAgent",null);
            custom.start();            
           
            out.println("我已经启动了一个小Agent");
       
        }
        catch (Exception e){
            e.printStackTrace();
        }
       
        out.println(" ");
        out.println("");
        
        out.close();
    }
   
    //
    /** Handles the HTTP GET method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
   
    /** Handles the HTTP POST method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
   
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    //
}
CustomAgent.java:
package examples;
import jade.core.*;
import jade.core.behaviours.*;
/**
 *
 * @author Administrator
 */
public class CustomAgent extends Agent{
     
    public void setup(){
        SimpleBehaviour helloBehaviour=new SimpleBehaviour(this) {
            boolean finished=false;
            public void action() {
                System.out.println("Hello World Behaviour run: 你好,世界!");
                System.out.println("-----我是:-----");
                System.out.println("我的本地名称是:"+getLocalName());
                System.out.println("我全局唯一的标志名称为:"+getName() );
                finished=true;
                try{
                        Thread.sleep(40000);
                }
                catch(java.lang.InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println("已经过了40秒钟");
//这里是为了测试关掉IE之后控制台上还会不会输出信息。
            }
            public boolean done() {
                return finished;
            }
           
        };
        addBehaviour(helloBehaviour);
    }
   
}
web.xml:
   
        StartServlet
        examples.StartServlet
   
   
        StartServlet
        /StartServlet
   
   
       
            30
       
   
   
      
            index.jsp
       
   
url:中看到如下结果:
Servlet StartServlet at /JSPTest
你好 运行Agent容器 ...(Profile main=true local-host=59.73.87.114 port=8888 services=jade.core.mobility.AgentMobilityService;jade.core.event.NotificationService host=59.73.87.114 local-port=8888 mtps=[jade.mtp.http.MessageTransportProtocol] jvm=j2se) 我已经启动了一个小Agent
Tomcat控制台上输出信息:
Hello World Behaviour run: 你好,世界!
-----我是:-----
我的本地名称是:custom
我全局唯一的标志名称为:custom@deepin-84e2a07b:8888/JADE
已经过了40秒钟
同时图形化JADE容器运行,其中“已经过了40秒钟”是在浏览器关闭之后显示,说明Agent容器及Agent启动后一直运行着。
奇怪的是,我每次退出主容器,总显示
Exception while removing reference: java.lang.InterruptedException
java.lang.InterruptedException
        at java.lang.Object.wait(Native Method)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:116)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:132)
        at sun.java2d.Disposer.run(Disposer.java:107)
        at java.lang.Thread.run(Thread.java:595)
AWT blocker activation interrupted: //后面这个是JADE GUIbug么?
java.lang.InterruptedException
        at java.lang.Object.wait(Native Method)
        at java.lang.Object.wait(Object.java:474)
        at sun.awt.AWTAutoShutdown.activateBlockerThread(AWTAutoShutdown.java:309)
        at sun.awt.AWTAutoShutdown.setToolkitBusy(AWTAutoShutdown.java:226)
        at sun.awt.AWTAutoShutdown.notifyToolkitThreadBusy(AWTAutoShutdown.java:118)
        at sun.awt.windows.WToolkit.eventLoop(Native Method)
        at sun.awt.windows.WToolkit.run(WToolkit.java:269)
        at java.lang.Thread.run(Thread.java:595)
不知道什么问题?
阅读(4732) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

chinaunix网友2010-03-27 19:59:17

我有个问题想请教,就是在servlet中调用jade,二者共用一个JVM可以吗?我在调用的时候,为什么总是提示说找不到jade.core.Profile类呢