public Result morphium() { System.out.println("Inside play productcontroller morphium action"); StringBuffer sb = new StringBuffer(); try { MorphiumConfig cfg = new MorphiumConfig(); cfg.setDatabase("play"); // db name is play cfg.addHost("localhost", 27017); Morphium m = new Morphium(cfg); Query<Item> q = m.createQueryFor(Item.class).f("name").eq("mac"); List<Item> lst = q.asList(); for (Item item : lst) { System.out.println(item); sb.append(item.toString() + "<br>\n"); } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ok("test morphium <br>\n" + sb.toString()); }
@Inject public MorphiumConnection(ApplicationLifecycle lifecycle, Configuration configuration) { final String dbName = configuration.getString("mongodb.name"); final String dbHostName = configuration.getString("mongodb.host.name"); final Integer dbHostPort = configuration.getInt("mongodb.host.port"); final String username = configuration.getString("mongodb.username"); final String password = configuration.getString("mongodb.password"); Logger.info( "Mong db name: " + dbName + ", host : " + dbHostName + ", port : " + dbHostPort + ", username:"******", passsword:" + password); cfg = new MorphiumConfig(); cfg.setDatabase(dbName); try { cfg.addHost(dbHostName, dbHostPort); if (StringUtils.isNotEmpty(username)) { cfg.setMongoLogin(username); } if (StringUtils.isNotEmpty(password)) { cfg.setMongoPassword(password); } m = new Morphium(cfg); } catch (UnknownHostException e) { e.printStackTrace(); m = null; cfg = null; } lifecycle.addStopHook( () -> { if (m != null) { m.close(); } return F.Promise.pure(null); }); }