2017-08-29 80 views
0

我是java新手,想從mongoDB集合中讀取隨機數據,並通過websockets將其發送給一個簡單的websocket客戶端。其實我已經運行了一個部署我的Java應用程序(websocketserver)的tomcat。這是一個簡單的websocket回顯服務器,它回覆來自客戶端的消息。如何從mongoDB中讀取文檔並通過java中的websockets發送文檔?

 package websocket.chat; 


     import com.mongodb.MongoClient; 
     import javax.websocket.*; 
     import javax.websocket.server.*; 
     import java.io.IOException; 


     @ServerEndpoint(value="/echo") 
     public class EchoEndpoint { 

      MongoAccess mongo; 


      @OnOpen 
      public void onOpen(Session session, EndpointConfig config) throws IOException { 
       System.out.println("open"); 
    mongo = new MongoAccess(new MongoClient("localhost",27017)); 



    session.getBasicRemote().sendText(mongo.readNumberOfDocuments(10).toString()); 



      } 

      @OnMessage 
      public String echo(String message) { 

       System.out.println("Server answer"); 
       return message + " (from your server)"; 

      } 

      @OnError 
      public void onError(Throwable t) { 

       System.out.println("Error!!!"); 
       t.printStackTrace(); 
      } 

      @OnClose 
      public void onClose(Session session) { 



       System.out.println("close"); 

      } 
     } 

現在我想發送來自mongoDB的OnMessage事件數據。爲此我創建了三個類。

類Nr.1從集合

package websocket.chat; 


import com.mongodb.*; 
import com.mongodb.client.MongoCollection; 
import com.mongodb.client.MongoCursor; 
import com.mongodb.client.MongoDatabase; 
import org.bson.Document; 

import java.net.UnknownHostException; 


import java.util.ArrayList; 
import java.util.List; 

public class MongoAccess { 


    private DBCollection collection; 



    //Konstruktor 
    public MongoAccess(MongoClient mongo) 
    { 
     this.collection = mongo.getDB("randomData").getCollection("random"); 
    } 


    //read all datasets 
    public List<Dataset> readAllDocuments(){ 

     List<Dataset> data = new ArrayList<Dataset>(); 

     //Cursor object to iterate over set of documents 
     DBCursor cursor = collection.find(); 

      while(cursor.hasNext()){ 
       System.out.println(cursor.next()); 
       DBObject doc = cursor.next(); 
       Dataset d = DataConverter.convertDBobject(doc); 
       data.add(d); 

      } 
     return data; 

    } 


    //read a number of datasets 
    public List<Dataset> readNumberOfDocuments(int number) { 


     //query, less then equal number 
     BasicDBObject query = new BasicDBObject(("data_id"), new BasicDBObject("$lte", number)); 

     //Cursor object to iterate over set of documents that matched our query 
     DBCursor cursor = collection.find(query); 

     List<Dataset> data = new ArrayList<Dataset>(); 


      while (cursor.hasNext()) { 
       System.out.println(cursor.next()); 
       DBObject doc = cursor.next(); 
       Dataset d = DataConverter.convertDBobject(doc); 
       data.add(d); 
      } 

     return data; 

    } 

} 

類NR 2讀出的數據:所述數據對象

package websocket.chat; 


public class Dataset { 

    private Integer data_id; 
    private Float X; 
    private Float Y; 
    private Float Z; 

    public Integer getData_id() { 
     return data_id; 
    } 

    public void setData_id(Integer data_id) { 
     this.data_id = data_id; 
    } 

    public Float getX() { 
     return X; 
    } 

    public void setX(Float x) { 
     X = x; 
    } 

    public Float getY() { 
     return Y; 
    } 

    public void setY(Float y) { 
     Y = y; 
    } 

    public Float getZ() { 
     return Z; 
    } 

    public void setZ(Float z) { 
     Z = z; 
    } 


} 

類Nr.3:轉換DBOBJECT

import com.mongodb.BasicDBObjectBuilder; 



public class DataConverter { 

    //convert DBObject to Dataset Object 
    public static Dataset convertDBobject(DBObject doc) 
    { 
     Dataset d = new Dataset(); 
     d.setData_id((int)doc.get("data_id")); 
     d.setX((float)doc.get("x")); 
     d.setY((float)doc.get("y")); 
     d.setZ((float)doc.get("z")); 
     return d; 

    } 

} 

當我嘗試連接到websocket服務器時,websocket連接會拋出以下錯誤:

java.lang.NoClassDefFoundError: com/mongodb/DBObject 
    at websocket.chat.EchoEndpoint.onOpen(EchoEndpoint.java:22) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.apache.tomcat.websocket.pojo.PojoEndpointBase.doOnOpen(PojoEndpointBase.java:65) 
    at org.apache.tomcat.websocket.pojo.PojoEndpointServer.onOpen(PojoEndpointServer.java:64) 
    at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:133) 
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:844) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1410) 
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.ClassNotFoundException: com.mongodb.DBObject 
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285) 
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119) 
    ... 15 more 

我試圖找到錯誤,但我沒有東西。有任何想法嗎?

回答

0

我發現我的錯誤。我使用tomcat來部署應用程序。所以我忘了將mongo-java-driver.jar添加到tomcat的lib文件夾中。因爲tomcat服務器在運行時間內找不到該類。