Java를 사용하여 서비스에 액세스

[Microsoft 에이전트는 Windows 7을 기준으로 더 이상 사용되지 않으며 이후 버전의 Windows에서는 사용할 수 없습니다.]

Java 애플릿에서 Microsoft 에이전트 서비스에 액세스할 수도 있습니다. Microsoft 에이전트 인터페이스를 통해 액세스할 수 있는 많은 함수는 참조로 전달된 매개 변수를 통해 값을 반환합니다. Java에서 이러한 매개 변수를 전달하려면 코드에서 단일 요소 배열을 만들고 적절한 함수에 매개 변수로 전달해야 합니다. Microsoft Visual J++를 사용하고 Microsoft 에이전트 서버에서 Java 형식 라이브러리 마법사를 실행한 경우 summary.txt 파일을 참조하여 배열 인수가 필요한 함수를 검토합니다. 프로시저는 C의 프로시저와 비슷합니다. IAgentEx 인터페이스를 사용하여 서버의 instance 만든 다음 문자를 로드합니다.

private IAgentEx            m_AgentEx = null;
private IAgentCharacterEx   m_Merlin[] = {null};
private int                 m_MerlinID[] = {-1};
private int                 m_RequestID[] = {0};
private final String        m_CharacterPath = "merlin.acs";

public void start()
{
      // Start the Microsoft Agent Server

      m_AgentEx = (IAgentEx) new AgentServer();

      try
      {

         Variant characterPath = new Variant();
         characterPath.putString(m_CharacterPath);

         // Load the character

         m_AgentEx.Load(characterPath,
                    m_MerlinID,
                    m_RequestID);
      }

이 절차는 웹 사이트와 같은 HTTP 원격 위치에서 문자를 로드할 때 약간 다릅니다. 이 경우 Load 메서드는 비동기적이며 E_PENDING(0x8000000a)의 COM 예외를 발생합니다. 다음 함수에서와 같이 이 예외를 catch하고 올바르게 처리해야 합니다.

// Constants used in asynchronous character loads

private final int E_PENDING = 0x8000000a;
private final int NOERROR = 0;


// This function loads a character from the specified path.
// It correctly handles the loading of characters from
// remote sites.

// This sample doesn't care about the request id returned
// from the Load call.  Real production code might use the
// request id and the RequestComplete callback to check for
// a successful character load before proceeding.

public int LoadCharacter(Variant path, int[] id)
{
   int requestid[] = {-1};
   int hRes = 0;

   try
   {
      // Load the character

      m_AgentEx.Load(path, id, requestid);
   }
   catch(com.ms.com.ComException e)
   {
      // Get the HRESULT

      hRes = e.getHResult();
      
      // If the error code is E_PENDING, we return NOERROR

      if (hRes == E_PENDING)
         hRes = NOERROR;
   }

   return hRes;
}

public void start()
{
   if (LoadCharacter(characterPath, m_MerlinID) != NOERROR)
   {
      stop();
      return;
   }

   // Other initialization code here

   .
   .
   .
}

그런 다음, 메서드에 액세스할 수 있는 IAgentCharacterEx 인터페이스를 가져옵니다.

// Get the IAgentCharacterEx interface for the loaded
// character by passing its ID to the Agent server.

m_AgentEx.GetCharacterEx(m_MerlinID[0], m_Merlin);

// Show the character

m_Merlin[0].Show(FALSE, m_RequestID);

// And speak hello

m_Merlin[0].Speak("Hello World!", "", m_RequestID);

마찬가지로 이벤트에 대한 알림을 받려면 IAgentNotifySink 또는 IAgentNotifySinkEx 인터페이스를 구현하여 해당 형식의 개체를 만들고 등록해야 합니다.

...
// Declare an Agent Notify Sink so that we can get
// notification callbacks from the Agent server.

private AgentNotifySinkEx m_SinkEx = null;
private int            m_SinkID[] = {-1};

public void start()
   {
   ...
   // Create and register a notify sink

   m_SinkEx = new AgentNotifySinkEx();

   m_AgentEx.Register(m_SinkEx, m_SinkID);
   …
   // Give our notify sink access to the character

   m_SinkEx.SetCharacter(m_Merlin[0]);
   ...
   }

Java 애플릿에서 Microsoft 에이전트에 액세스하려면 애플릿을 사용하여 설치하는 Java 클래스를 생성해야 합니다. 예를 들어 Visual J++ Java 형식 라이브러리 마법사를 사용하여 이러한 파일을 생성할 수 있습니다. 웹 페이지에서 애플릿을 호스트하려는 경우 페이지와 함께 다운로드하는 생성된 클래스 파일을 포함하는 서명된 Java CAB를 빌드해야 합니다. 클래스 파일은 Java 샌드박스 외부에서 실행되는 COM 개체이므로 Microsoft 에이전트 서버에 액세스하는 데 필요합니다.