我写了一个授权系统,它依赖于代表当前用户的对象.为了简化编程并提高性能,我希望在用户登录后将这些对象保存在ThreadLocal中.
它看起来像这样:
public class UserCache {
private static final ThreadLocal<User> cache = new ThreadLocal<User>();
public User getCurrentUser() {
return cache.get();
}
public void setCurrentUser(User user) {
cache.set(user);
}
}
Run Code Online (Sandbox Code Playgroud)
我已经读过静态元素使聚类成问题.如果我在每个群集节点上都有一个UserCache,则它们都有自己的缓存对象与其他节点上的缓存对象不同步.对?UserCache是单例的经典候选者,因为应用程序只需要它的一个实例.但据我所知,@ Singleton EJB在集群中具有相同的行为.
那么如何使UserCache在EJB 3.1(Java EE 6)环境中可以集群?
从答案中提取的解决方案:
我想使用以下参数构造一个多部分请求:name(字符串),email(字符串)和fileupload(文件).我正在使用下面的Java代码(在Android中工作).
打印httppost.getRequestLine()
POST http://www.myurl.com/upload HTTP/1.1
Run Code Online (Sandbox Code Playgroud)
所以在客户端站点上一切看起来都不错,但我的服务器(Django/Apache)将其作为GET请求读取,没有GET参数 - request.method生成'GET',request.GET.items()生成一个空字典.
我究竟做错了什么?我实际上并不知道如何正确设置多部分参数 - 使用猜测 - 所以这可能是问题所在.
public void SendMultipartFile() {
Log.e(LOG_TAG, "SendMultipartFile");
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.myurl.com/upload");
File file = new File(Environment.getExternalStorageDirectory(),
"video.3gp");
Log.e(LOG_TAG, "setting up multipart entity");
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("fileupload", cbFile);
Log.i("SendLargeFile", "file length = " + file.length());
try {
mpEntity.addPart("name", new StringBody(name));
mpEntity.addPart("email", new StringBody(email));;
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block …Run Code Online (Sandbox Code Playgroud) 我想将模块用作其他模块中引用的单例.它看起来像这样(这实际上不是我正在研究的代码,但我简化它以丢弃所有不相关的东西):
main.py
import singleton
import printer
def main():
singleton.Init(1,2)
printer.Print()
if __name__ == '__main__':
pass
Run Code Online (Sandbox Code Playgroud)
singleton.py
variable1 = ''
variable2 = ''
def Init(var1, var2)
variable1 = var1
variable2 = var2
Run Code Online (Sandbox Code Playgroud)
printer.py
import singleton
def Print()
print singleton.variable1
print singleton.variable2
Run Code Online (Sandbox Code Playgroud)
我希望得到输出1/2,但相反得到空的空间.据我所知,在将singleton导入print.py模块后,变量再次初始化.
所以我认为我必须在singleton.py之前检查它们是否已初具化:
if not (variable1):
variable1 = ''
if not (variable2)
variable2 = ''
Run Code Online (Sandbox Code Playgroud)
但我不知道该怎么做.或者有更好的方法在python中使用我不知道的单例模块:)
是否有用于从Mobile Safari访问iPhone相机的JavaScript API?
在每次提交后,我都没有找到使用SVN从远程服务器部署到远程服务器的解决方案.如何通过FTP上传到服务器的唯一添加或编辑的文件,并从SVN删除已删除的文件我想写写后提交脚本...
我对TFS比较新,我想知道其他人在组织有很多项目的TFS吗?例如,有人知道是否可以将TFS项目放在文件夹中或者人们使用前缀/后缀?
我的任务是从一些奇怪的旧系统中读取一些数据.
系统包含许多日期,但它们都是奇怪的格式.它们是从大约55,000到大约的整数80,000.
我知道两个确定日期:
58,112 equals February 5, 1960 (originally written as Feb 2,1960 [*])
61,439 equals March 16, 1969
[*] This typo explains some of the comments initially challenging the
leap-year awareness of the calendar.
Run Code Online (Sandbox Code Playgroud)
在我看来,这些整数是自1800年12月28日以来经过的天数.但我认为这是开始日历的一个非常奇怪的日期.闰年可能正在发生什么事情,什么 - 不会在以后引起问题.
有人认出这个日历吗?任何人都可以告诉我将这些整数转换为人类可读日期的正确方法是什么?
我们使用以下函数自动检测我们是在机器内部还是在实时服务器上,然后为各种组件选择适当的配置:
function devIsLocal(){
$res=false;
$http_host=$_SERVER['HTTP_HOST'];
if($http_host=='localhost')$res=true;
if($http_host=='127.0.0.1')$res=true;
if(substr($http_host,-4)=='.lan')$res=true;
if(strpos($http_host, '.')===false)$res=true;
return($res);
}
Run Code Online (Sandbox Code Playgroud)
如您所见,它仅依赖于HTTP_HOST值.
当然,如果您在本地使用某种类型的虚拟主机,例如example.com,那么该功能将被欺骗.
有没有其他方法来欺骗这个功能?还有哪些其他变量/地方可以用来确定我们的位置?
php development-environment production-environment autodiscovery
我的ASP.NET应用程序读取xml文件以确定它当前所处的环境(例如本地,开发,生产).
它每次打开与数据库的连接时都会检查此文件,以便知道从应用程序设置中获取哪个连接字符串.
我正在进入一个发展阶段,效率正在成为一个问题.我不认为在我希望访问数据库的过程中(非常经常)读取物理磁盘上的文件是个好主意.
我正在考虑在Application ["ConnectionString"]中存储连接字符串.所以代码就是
public static string GetConnectionString
{
if (Application["ConnectionString"] == null)
{
XmlDocument doc = new XmlDocument();
doc.Load(HttpContext.Current.Request.PhysicalApplicationPath + "bin/ServerEnvironment.xml");
XmlElement xe = (XmlElement) xnl[0];
switch (xe.InnerText.ToString().ToLower())
{
case "local":
connString = Settings.Default.ConnectionStringLocal;
break;
case "development":
connString = Settings.Default.ConnectionStringDevelopment;
break;
case "production":
connString = Settings.Default.ConnectionStringProduction;
break;
default:
throw new Exception("no connection string defined");
}
Application["ConnectionString"] = connString;
}
return Application["ConnectionString"].ToString();
}
Run Code Online (Sandbox Code Playgroud)
我没有设计应用程序所以我认为每次都必须有一个读取xml文件的原因(在应用程序运行时更改设置?)我对这里的内部工作概念很少.优缺点都有什么?您是否认为通过实施上述功能可以获得较小的性能提升?
谢谢
这不是有效的代码:
public class MyClass
{
private static boolean yesNo = false;
static
{
if (yesNo)
{
System.out.println("Yes");
return; // The return statement is the problem
}
System.exit(0);
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个愚蠢的例子,但在静态类构造函数中我们不能return;.为什么?这有充分的理由吗?有人知道更多关于此的事情吗?
所以我应该做的return就是结束那里的建设.
谢谢
java ×2
php ×2
android ×1
asp.net ×1
calendar ×1
camera ×1
datetime ×1
declaration ×1
django ×1
ejb-3.0 ×1
file-upload ×1
ftp ×1
httpclient ×1
iphone ×1
java-ee ×1
javascript ×1
leap-year ×1
multipart ×1
performance ×1
python ×1
return ×1
singleton ×1
svn ×1
team-project ×1
tfs ×1
thread-local ×1
variables ×1