MongoDB
 sql >> डेटाबेस >  >> NoSQL >> MongoDB

MongoDb jndi . के माध्यम से

हां, यह संभव है, जब आप अपनी खुद की जेएनडीआई फैक्ट्री बना सकते हैं तो किसी और कोड पर भरोसा क्यों करें? बस एक वर्ग बनाएं जो javax.naming.spi.ObjectFactory और एक बीन लागू करता है जो जेएनडीआई संदर्भ से मोंगो खींचता है, मैंने इसे वसंत डेटा के लिए कॉन्फ़िगर किया है -मोंगो मोंगोटेम्पलेट ऑब्जेक्ट।

public class CustomMongoJNDIFactory implements ObjectFactory {

public Object getObjectInstance(Object obj, Name name, Context nameCtx,
        Hashtable<?, ?> environment) throws Exception {

    validateProperty(obj, "Invalid JNDI object reference");

    MongoTemplate mongoTemplate = null;
    String db = null;
    String host = null;
    String username = null;
    String password = null;
    int port = 27017;

    Reference ref = (Reference) obj;
    Enumeration<RefAddr> props = ref.getAll();
    while (props.hasMoreElements()) {
        RefAddr addr = (RefAddr) props.nextElement();
        String propName = addr.getType();
        String propValue = (String) addr.getContent();
        if (propName.equals("db")) {
            db = propValue;
        } else if (propName.equals("host")) {
            host = propValue;
        } else if (propName.equals("username")) {
            username = propValue;
        } else if (propName.equals("password")) {
            password = propValue;
        } else if (name.equals("port")) {
            try {
                port = Integer.parseInt(propValue);
            } catch (NumberFormatException e) {
                throw new NamingException("Invalid port value " + propValue);
            }
        }

    }

    // validate properties
    validateProperty(db, "Invalid or empty mongo database name");
    validateProperty(host, "Invalid or empty mongo host");
    validateProperty(username, "Invalid or empty mongo username");
    validateProperty(password, "Invalid or empty mongo password");

    //create mongo template
    mongoTemplate = new MongoTemplate(new Mongo(host, port), db,
            new UserCredentials(username, password));

    return mongoTemplate;
}


/**
 * Validate internal String properties
 * 
 * @param property
 * @param errorMessage
 * @throws NamingException
 */
private void validateProperty(String property, String errorMessage)
        throws NamingException {
    if (property == null || property.trim().equals("")) {
        throw new NamingException(errorMessage);
    }
}

/**
 * Validate internal Object properties
 * 
 * @param property
 * @param errorMessage
 * @throws NamingException
 */
private void validateProperty(Object property, String errorMessage)
        throws NamingException {
    if (property == null) {
        throw new NamingException(errorMessage);
    }
}

}

स्प्रिंग बीन:

@Configuration
@Qualifier("mongoTemplate")
public class CustomMongoTemplate  {


 public @Bean MongoTemplate mongoTemplate() throws Exception {
     Context initCtx = new InitialContext();
     Context envCtx = (Context) initCtx.lookup("java:comp/env");
     return (MongoTemplate) envCtx.lookup("bean/MyMongoBean");
    }
}

प्रसंग.एक्सएमएल:

<Resource name="bean/MyMongoBean" auth="Container"
        type="org.springframework.data.mongodb.core.MongoTemplate"
        factory="com.package.CustomMongoJNDIFactory"
        host="" db="" username="" password=""/>

वेब.एक्सएमएल

    <resource-env-ref>
    <description>Mongo JNDI configuration</description>
    <resource-env-ref-name>comp/env/bean/MyMongoBean</resource-env-ref-name>
    <resource-env-ref-type>org.springframework.data.mongodb.core.MongoTemplate</resource-env-ref-type>
</resource-env-ref>


  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. मोंगोडब नेवला में E11000 डुप्लिकेट कुंजी त्रुटि सूचकांक

  2. जावा में बीएसओएन से जेएसओएन दस्तावेज़ रूपांतरण

  3. _id का उपयोग करके खोजें जो एकत्रीकरण के साथ काम नहीं कर रहा है

  4. MongoDB में सर्वर-साइड संग्रहीत फ़ंक्शंस का उपयोग करने की अनुशंसा क्यों नहीं की जाती है?

  5. MongoDB E11000 डुप्लिकेट कुंजी त्रुटि